I just noticed that the universal newline feature of file operations seems to be on its way out.

The documentation for Python 3.5 open's mode parameter indicates that it's deprecated:

'U' universal newlines mode (deprecated)

At least as far back as Python 3.2, open contains a similar "backwards compatibility only" warning when documenting the usage of the mode argument:

'U' universal newlines mode (for backwards compatibility; should not be used in new code)

Even in Python 2.7, a similar warning is placed in the documentation of io.open.

What's the reason for this?

  • 2
    The logic behind this is fairly simple. It's considered more "Pythonic" to have named things rather than unnamed things. So you use a named parameter rather than a character flag. The flag idea is very much a leftover of Python's C implementation and it's small wonder that it's being weeded out.
    – user28988
    Oct 1 '15 at 20:58
  • Because files are opened in universal newline mode by default.
    – Boris
    Dec 27 '18 at 9:11

The open() function in the Python 3 library has a newline argument. Setting it to None enables universal newlines. This is the accepted way to do it, rendering the mode='U' argument redundant.

Use newline=None to enable universal newlines mode (this is the default).

  • 5
    Looks like I misunderstood. I read it as saying that universal newlines in general were being deprecated, not just the mode character. Thanks for clearing that up.
    – jpmc26
    Oct 1 '15 at 20:21

After stumbling across this question, I updated the documentation to be clearer about what's going on (https://github.com/python/cpython/pull/11646/files).

The confusingly cryptic table entry for 'U' is gone, and instead there's a paragraph further down that states:

There is an additional mode character permitted, 'U', which no longer has any effect, and is considered deprecated. It previously enabled :term:universal newlines in text mode, which became the default behaviour in Python 3.0. Refer to the documentation of the :ref:newline <open-newline-parameter> parameter for further details.

  • 2
    An answer that references an upstream patch in the docs by the author - the right way for stackexchanges to work. Kudos @ncoghlan.
    – qneill
    Feb 4 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.