altering an object as you iterate over it?

John Salerno johnjsal at NOSPAMgmail.com
Fri May 19 22:12:48 CEST 2006


bruno at modulix wrote:

> Now if what you want to do is just to rewrite the file without the blank
> files, you need to use a second file:
> 
> fin = open(path, 'r')
> fout = open(temp, 'w')
> for line in fin:
>   if line.strip():
>     fout.write(line)
> fin.close()
> fout.close()
> 
> then delete path and rename temp, and you're done. And yes, this is
> actually the canonical way to do this !-)

Thanks, that's what I want. Seems a little strange, but at least you 
showed me that line.strip() is far better than line == '\n'



More information about the Python-list mailing list