Writelines() a bit confusing

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat May 19 13:57:26 EDT 2007


En Sat, 19 May 2007 10:31:50 -0300, Stefan Sonnenberg-Carstens  
<stefan.sonnenberg at pythonmeister.com> escribió:

> so you'd want this:
>
> f.writelines([x+os.linesep for x in strings])
>
> or something similar.

You would use os.linesep *only* if the file was opened in binary mode -  
unusual if you want to write lines of text.
For a file opened in text mode (the default) the line terminator is always  
'\n' - let Python handle the platform differences. On Windows you would  
end with malformed or duplicate line terminators if you use explicitely  
os.linesep when writing.
See http://mail.python.org/pipermail/python-list/2000-May/037191.html

-- 
Gabriel Genellina




More information about the Python-list mailing list