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?

asked Sep 30, 2015 at 23:53
3
  • 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. Commented Oct 1, 2015 at 20:58
  • Because files are opened in universal newline mode by default. Commented Dec 27, 2018 at 9:11
  • The U flag to open's mode parameter is deprecated in Python 3.3 and removed entirely in Python 3.11 Commented Jun 15, 2023 at 21:40

2 Answers 2

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

answered Oct 1, 2015 at 2:14
1
  • 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. Commented Oct 1, 2015 at 20:21
13

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.

answered Jan 27, 2019 at 16:26
3
  • 3
    An answer that references an upstream patch in the docs by the author - the right way for stackexchanges to work. Kudos @ncoghlan. Commented Feb 4, 2021 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). Commented Dec 6, 2022 at 14:10
  • @HelmutWollmersdorfer newline='' is likely the option you want: stackoverflow.com/questions/5144382/… Commented Dec 16, 2022 at 4:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.