The basic strategy is to write a copy of the input file to the output file, but with changes. In your case, the changes are very simple: you just omit the lines you don't want.
Once you have your copy safely written, you can delete the original file and use 'os.rename()' to rename your temp file to the original file name. I like to write the temp file in the same directory as the original file, to make sure I have permission to write in that directory and because I don't know if os.rename()
can move a file from one volume to another.
You don't need to say for line in fin.readlines()
; it is enough to say for line in fin
. When you use .readlines()
you are telling Python to read every line of the input file, all at once, into memory; when you just use fin
by itself you read one line at a time.
Here is your code, modified to do these changes.
sourcefile = "C:\\Python25\\PC_New.txt"
filename2 = "C:\\Python25\\PC_reduced.txt"
offending = ["Exception","Integer","RuntimeException"]
def line_offends(line, offending):
for word in line.split():
if word in offending:
return True
return False
def fixup( filename ):
print "fixup ", filename
fin = open( filename )
fout = open( filename2 , "w")
for line in fin:
if line_offends(line, offending):
continue
fout.write(line)
fin.close()
fout.close()
#os.rename() left as an exercise for the student
fixup(sourcefile)
If line_offends()
returns True, we execute continue
and the loop continues without executing the next part. That means the line never gets written. For this simple example, it would really be just as good to do it this way:
for line in fin:
if not line_offends(line, offending):
fout.write(line)
I wrote it with the continue
because often there is non-trivial work being done in the main loop, and you want to avoid all of it if the test is true. IMHO it is nicer to have a simple "if this line is unwanted, continue" rather than indenting a whole bunch of stuff inside an if
for a condition that might be very rare.