Python script to remove lines from file containing words in array
Posted
by chromesub
on Stack Overflow
See other posts from Stack Overflow
or by chromesub
Published on 2010-06-15T06:00:06Z
Indexed on
2010/06/15
6:12 UTC
Read the original article
Hit count: 140
I have the following script which identifies lines in a file which I want to remove, based on an array but does not remove them.
What should I change?
sourcefile = "C:\\Python25\\PC_New.txt"
filename2 = "C:\\Python25\\PC_reduced.txt"
offending = ["Exception","Integer","RuntimeException"]
def fixup( filename ):
print "fixup ", filename
fin = open( filename )
fout = open( filename2 , "w")
for line in fin.readlines():
for item in offending:
print "got one",line
line = line.replace( item, "MUST DELETE" )
line=line.strip()
fout.write(line)
fin.close()
fout.close()
fixup(sourcefile)
© Stack Overflow or respective owner