Delete a certain line from a file

Cesar Rabak csrabak at uol.com.br
Tue Feb 13 15:56:45 CET 2001


Gustaf Liljegren wrote:
> 
> 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?

Unfortunately no ;-)

If you feel you'll need this kind of functionality more often than not,
you should consider changing from a textfile to a database.

HTH

Cesar



More information about the Python-list mailing list