Delete a certain line from a file

Gustaf Liljegren gustafl at algonet.se
Tue Feb 13 07:35:31 EST 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
    break

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?

Regards,

Gustaf Liljegren





More information about the Python-list mailing list