skip to main |
skip to sidebar
Find and delete string in text file
import sys
# input a original file
inp = open("december.txt","r")
#write to a new file
outp = open("newDecember.txt","w")
#declare global
lines = inp.readlines()
for line in lines:
#find the string
if(line.find("asfgsfhhshas")!=-1):
## print line
#write to a new file
sys.stdout.write(line)
outp.write(line)
inp = open("december.txt","r")
badf = open("badDecember.txt","w")
goodf = open("goodDecember.txt",'w')
lines = inp.readlines()
for line in lines:
if line.find("asfgsfhhshas") != -1:
sys.stdout.write(line)
badf.write(line)
else:
goodf.write(line)
badf.close()
goodf.close()
inp.close()
1 comment:
Thanks, this helped me a lot :)
Post a Comment