Strange behaviour with os.linesep

Chris Angelico rosuav at gmail.com
Wed Jul 24 03:39:26 EDT 2013


On Wed, Jul 24, 2013 at 5:02 PM, Vincent Vande Vyvre
<vincent.vandevyvre at swing.be> wrote:
> In fact, in my code, the original file is open in binary mode, the line
> separator is translate to \n and it is parsed by the module tokenise.
>
> I'm not a Windows user but my code must be run also on Win, this is the
> reason of the usage of os.linesep in writting.
>
> So, now I found the solution, just write the file in binary mode and now it
> is correctly open with all the editors.
>

Sounds to me like the problem was double-translation - you in your
code turned \n into \r\n, but then the file was written in text mode,
doing the same translation, so your lines were terminated with \r\r\n.
That would result in what you're seeing (including the oddity that
some editors will show the file differently).

You may find it easier to write the file in text mode and let the
underlying system do the translation for you. Chances are that'll be
correct.

ChrisA



More information about the Python-list mailing list