file operations

Delaney, Timothy tdelaney at avaya.com
Tue Oct 30 18:51:35 EST 2001


> From: Eyal Lotem [mailto:eyal at hyperroll.com]
> Kerstin wrote:
> 
> > begin loop
> > - read file
> > - replace something in the file (seem to work)
> > - write file
> > end loop
> > 
> > This has to be done quite often.
> 
> A nice approach to this (if the files are not too large), is 
> to replace the 
> loop with something like:
> 
> open('filteredfile.output', 'w').writelines(
>         map(lambda line: line.rstrip() + '\n',
>                 open('inputfile', 'r').readlines()))

That's an evil thing to do to someone asking you to do their homework ... :)

Also, it's not a good thing to do in general, as not all implementations of
Python immediately call the finalizer of objects with a refcount of zero
(e.g. Jython).

Tim Delaney




More information about the Python-list mailing list