Python thinks file is empty

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Nov 5 15:07:54 EST 2007


En Mon, 05 Nov 2007 12:57:15 -0300, Tim Chase  
<python.list at tim.thechases.com> escribió:

> To confuse matters, it happens to work in your example, because a
> string is an iterable that returns each character in that string
> as the result, so code like this
>
>    f.writelines('hello\n')
>
> is effectively doing something like this
>
>    f.write('h')
>    f.write('e')
>    f.write('l')
>    f.write('l')
>    f.write('o')
>    f.write('\n')
>
> (unless writelines is optimized to smarten up the code here)

As of 2.5.1, it's not :(
writelines is perhaps one of the worst used functions in Python. It'b been  
there for ages (since 1.5.2 at least) but a lot of people does not know it  
even exists (and roll their own, *always* slower; map(f.write, lines)  
appears to be the favorite spelling...) or, if they are aware of it, use  
it wrongly as you pointed out above.
Perhaps some of them come from Delphi/Pascal, looking for something  
similar to writeln, and writelines is the closest match...

-- 
Gabriel Genellina




More information about the Python-list mailing list