I\'m trying to delete a specific line that contains a specific string.
I\'ve a file called numbers.txt with the following content:
Just for fun, here's a two-liner to do it.
lines = filter(lambda x:x[0:-1]!="tom", open("names.txt", "r"))
open("names.txt", "w").write("".join(lines))
Challenge: someone post a one-liner for this.
You could also use the fileinput module to get arguably the most readable result:
import fileinput
for l in fileinput.input("names.txt", inplace=1):
if l != "tom\n": print l[:-1]