Reputation: 193
I have many files that I am running multiple regex matches against, and deleting stuff. It's working fine, but whenever there is a match, I get duplicate lines in the bottom that I have delete manually every time. What's going on?
for year in range (2004,2009):
path="/foo/bar/"+str(year)+"/"
dirs = os.listdir(path)
for i in range(0,len(dirs)):
original_file = os.path.join(path, dirs[i])
f=open(original_file,"r+")
text=f.read()
text=re.sub('FY91/92','',text)
f.seek(0)
f.write(text)
f.close()
Upvotes: 0
Views: 105
Reputation: 3518
How about changing to:
text=open(original_file).read()
text=re.sub('FY91/92','',text)
f = open(original_file, 'w')
f.write(text)
f.close()
This will overwrite the existing content of the file.
Upvotes: 1