[Python-au] Python os.linesep question
Stephen George
steve_geo at optusnet.com.au
Tue Dec 15 12:28:02 UTC 2009
Hi,
Have a question about use of os.linesep, as it seems to not work as
advertised, unless I am doing something wrong.
I've tried on windows, cygwin and linux.
I'm trying to write a text file and enforce '\r\n' for endofline from
both windows and cygwin environment, currently cygwin outputs '\n'
I'm under impression from [1] that I can change the value of os.linesep
to 'enforce' my requirement
I'm under impression from [2] that i should NOT use os.linesep directly
in my strings as a new line character
I'm under impression from [3] and [2] that when the lines are in memory
the endofline should aways be '\n' only.
The attached program shows my attempt at 'enforcing' os.linesep to a non
native line ending, but the written files always only contain the native
platforms endofline character.
What am I doing wrong?
Note: I have a working solution already where I force a change of new
line by using sting replace method on my strings before writing them, I
just don't understand why I can't get the 'advertised' feature to work?
- steve
======================
[1] from http://python.about.com/od/pythonstandardlibrary/ss/os-module-1.htm
*os.linesep:* The string used to indicate line breaks. On Unix-based
systems (Unix, Linux, Mac OS X), this is '\n'. However, if you want to
emulate a Windows system, you can change this to '\r\n' to represent the
carriage return and newline feed strings used on Windows platforms.
[2] from http://docs.python.org/library/os.html#os.linesep
*os.linesep: *The string used to separate (or, rather, terminate) lines
on the current platform. This may be a single character, such as '\n'
for POSIX, or multiple characters, for example, '\r\n' for Windows. Do
not use /os.linesep/ as a line terminator when writing files opened in
text mode (the default); use a single '\n' instead, on all platforms.
[3] Discussion about Universal mode indicates any format of new line in
a file will be translated to '\n' when it in memory
from http://docs.python.org/library/functions.html#open
Python is usually built with universal newline support; supplying 'U'
opens the file as a text file, but lines may be terminated by any of the
following: the Unix end-of-line convention '\n', the Macintosh
convention '\r', or the Windows convention '\r\n'. All of these external
representations are seen as '\n' by the Python program.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: linesep.py
Type: text/x-python
Size: 691 bytes
Desc: not available
URL: <http://starship.python.net/pipermail/python-au/attachments/20091215/54c677bd/attachment.py>
More information about the python-au
mailing list