What is the non deprecated version of open "U" mode

Question:

I am trying to read a textfile in python using:

with open("Keys.txt","rU") as csvfile:

however this produces a depreciation warning.

DeprecationWarning: 'U' mode is deprecated

What is the non deprecated version for this mode of access for a text/csv file.

Asked By: Lyra Orwell

||

Answers:

It’s the default behaviour now, so you can simply omit it:

with open("Keys.txt", "r") as csvfile:

From the open() documentation:

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

By the way, it’s being removed in Python 3.11.

See also: Why is universal newlines mode deprecated in Python? – Software Engineering Stack Exchange

Answered By: wjandrea
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.