Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why you shouldn't use os.linesep when editing on text mode?

Tags:

python

Python 2.7 documentation (and Python 3 documentation as well) contain the following line about the os.linepath function:

Do not use os.linesep as a line terminator when writing files opened in text mode (the default);

Why is that? And how is it different from using it on binary mode?

like image 964
Yam Mesicka Avatar asked Feb 07 '14 19:02

Yam Mesicka


1 Answers

When you open a file in text mode any \n that you write to the file is converted to the appropriate line ending for the platform you are using.

So, for example if you were on Windows where os.linesep is '\r\n', when you write that to a file the \n will get automatically converted to \r\n and you will end up with \r\r\n written to your file.

For example:

>>> import os
>>> os.linesep
'\r\n'
>>> with open('test.txt', 'w') as f:
...     f.write(os.linesep)
...
>>> with open('test.txt', 'rb') as f:
...     print repr(f.read())
...
'\r\r\n'
like image 55
Andrew Clark Avatar answered Sep 22 '22 03:09

Andrew Clark