Computer Science, asked by timung6191, 8 months ago

Remove all the lines that contain the character 'a' in a file and write it to another file

Answers

Answered by BushrajavediqbalKhan
1

Remove all the lines that contain the character 'a' in a file and write it to another file

Explanation:

import glob

# Merge all output files into one file

read_files = glob.glob('/home/user/Results/Script_tests/TestResults/*.output')

with open('MergedOutput.txt', 'r+b') as outfile:

   for file in read_files:

       with open(file, 'r+b') as infile:

           outfile.write(infile.read())

print 'Files merged.'          

# Remove header rows except from row 1

final_output = open('FinalMergedOutput.txt', 'r+b')

with open('MergedOutput.txt', 'r+b') as file:

   for line in file:

       if line == 0 and line.startswith('File'):

           final_output.write(line)

       elif line > 0 and not line.startswith('File'):

            final_output.write(line)  

for lineNumber, line in enumerate(file):

   if lineNumber == 0 and line.startswith('File'):

       final_output.write(line)

   elif lineNumber > 0 and not line.startswith('File'):

       final_output.write(line)

print 'Headers removed except on line 1.'

Answered by amuthamcablevkl
1

Answer:

infile = file('./oldfile.txt')

newopen = open('./newfile.txt', 'w')

for line in infile :

   if 'a' in line:

       line = line.replace('.' , '')

   else:

       newopen.write(line)

file.close()

Explanation:

plz mark me as brainest

Similar questions