To remove some lines from a file

Bruno Desthuilliers onurb at xiludom.gro
Wed Oct 25 16:20:06 CEST 2006

umut.tabak at wrote:
> Dear all,
> I am quite new to python. I would like to remove two lines from a file.

The canonical way to do so (at least for text files - which is the case
here) is :

open the source file in read mode
open a tmp file in write mode

for each line in source file:
  if this line should be kept:
     write it to the tmp file

close both files
replace source file with tmp file


> I would like to call this as
> myscript targetfile
> I have read sth about command line arguments. Perhaps I can cope with
> that myself but help is also appreciated on that.

import sys
print sys.argv

bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"

More information about the Python-list mailing list