29

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?

jpmc26
  • 5,489

2 Answers2

44

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).

Robert Harvey
  • 200,592
12

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.

Note: as of Python 3.11, this paragraph is no longer present in the docs, as the long-deprecated option has now been removed entirely.

ncoghlan
  • 3,609