all Python 3deprecated in 3.14codecsfunction
codecs.open()
stream codecs
Deprecated since 3.14: opens a file through a codecs stream codec. The replacement is the built-in open(path, mode, encoding=...), also known as io.open.
Syntax
codecs.open(filename, mode='r', encoding=None, errors='strict', buffering=-1)
It survives from Python 2, whose built-in open knew nothing about encodings. The differences are not in its favour: with an encoding given, the file is opened in binary mode, so '\r\n' is not turned into '\n', and you get a StreamReaderWriter instead of an io.TextIOWrapper. In old code replace it with open(path, encoding='utf-8') and check whether line parsing relied on that '\r'. Since 3.14 the call emits DeprecationWarning.
Examples
with open('a.txt', 'w', encoding='utf-8', newline='') as f:
f.write('a\r\nb\n')
with open('a.txt', encoding='utf-8') as f:
print(repr(f.read())) # → 'a\nb\n' — замена: встроенный open
import codecs, warnings
with open('a.txt', 'w', encoding='utf-8', newline='') as f:
f.write('a\r\nb\n')
warnings.simplefilter('ignore', DeprecationWarning)
with codecs.open('a.txt', encoding='utf-8') as f:
print(repr(f.read()), type(f).__name__) # → 'a\r\nb\n' StreamReaderWriter
See also
Open in the full glossarydocs.python.orgReport an inaccuracy