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,389
  • 4
  • 25
  • 37
  • 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. –  Oct 01 '15 at 20:58
  • Because files are opened in universal newline mode by default. – Boris Verkhovskiy Dec 27 '18 at 09:11
  • The `U` flag to `open`'s `mode` parameter is deprecated in Python 3.3 and [removed entirely in Python 3.11](https://docs.python.org/3/whatsnew/3.11.html#porting-to-python-3-11) – robertspierre Jun 15 '23 at 21:40

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

Eric O. Lebigot
  • 515
  • 2
  • 10
Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • 6
    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 01 '15 at 20:21
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,619
  • 1
  • 18
  • 13
  • 3
    An answer that references an upstream patch in the docs by the author - the right way for stackexchanges to work. Kudos @ncoghlan. – qneill Feb 04 '21 at 18:56
  • @ncoghlan It's still confusing and some what untechnical. In which mode are the newlines kept, if I need them to roundtrip? Does this mean, I need to read the text file as binary, if I want to keep the original newlines, then UTF-8 decode to a string, and then write it back as binary? IMHO it's too restrictive. Other script languages have ways to DWIM (Do What I Mean). – Helmut Wollmersdorfer Dec 06 '22 at 14:10
  • @HelmutWollmersdorfer `newline=''` is likely the option you want: https://stackoverflow.com/questions/5144382/preserve-end-of-line-style-when-working-with-files-in-python – ncoghlan Dec 16 '22 at 04:04