looping and deleting

Matt Gerrans mgerrans at mindspring.com
Mon Apr 21 18:44:51 CEST 2003

"Aahz" wrote:
> Are you trying to modify the file in-place?  If so, look at the
> fileinput module.  If not:

I thought the fileinput module was just for gathering all the files on the
command line into one virtual stream (that is, reading them all without
respect to filename, as one long series of lines).

I don't know if this is the best or most Pythonic way, but this is what I
usually do to modify a text file in place (unless I know it will be a really
big file):

lines = file( filename ).readlines()
f = file( filename, 'w' )
for line in lines:
   f.write( modifyTheLine(line) )

More information about the Python-list mailing list