[Python-au] Python os.linesep question
mhammond at skippinet.com.au
Tue Dec 15 13:00:44 UTC 2009
I haven't followed your references, but I'm pretty sure that the "mode"
of the file is handled by the C runtime library, not by Python - and
therefore os.linesep has no impact. Try opening your file with "wb"
mode, then you should see exactly what you write.
On 15/12/2009 11:28 PM, Stephen George wrote:
> 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  that I can change the value of os.linesep
> to 'enforce' my requirement
> I'm under impression from  that i should NOT use os.linesep directly
> in my strings as a new line character
> I'm under impression from  and  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
>  from
> *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.
>  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.
>  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.
> python-au maillist - python-au at starship.python.net
More information about the python-au