Delete a certain line from a file

Gustaf Liljegren gustafl at
Tue Feb 13 13:35:31 CET 2001

Excuse another newbie question. My script is reading a textfile with a list
of words (separated with newlines), and now I need something to delete a
certain record when I encounter it in a loop. Something like:

for line in f.readlines():
  if line == "somethingbad":
    # delete the whole line with newline

I have successfully done this by adding each line to a list and then used
list.remove("somethingbad"), but there's another way without having to put
the whole file in a list, isn't it?


Gustaf Liljegren

More information about the Python-list mailing list