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

后端 未结 1 1909
盖世英雄少女心
盖世英雄少女心 2021-01-08 00:06

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

Do not use os.li

相关标签:
1条回答
  • 2021-01-08 00:54

    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'
    
    0 讨论(0)
提交回复
热议问题