Python Glossary Русский

all Python 3deprecated in 3.10fileinputfunction

fileinput.hook_encoded()

opening hooks

Deprecated: builds an opening hook that reads files in the given encoding. Since 3.10 pass encoding to input() instead.

Syntax

fileinput.hook_encoded(encoding, errors=None)

Deprecated in 3.10, when input() and FileInput gained the encoding and errors parameters; on older versions it was the only way to set an encoding. Calling it emits no DeprecationWarning, so outdated code lives on unnoticed. You never need the hook and the encoding parameter together: the hook receives the encoding from input() anyway.

Examples

import fileinput
open('ru.txt', 'w', encoding='utf-8').write('привет\n')
# устаревший способ
with fileinput.input(['ru.txt'], openhook=fileinput.hook_encoded('utf-8')) as f:
    print(list(f))  # → ['привет\n']
# современный способ
with fileinput.input(['ru.txt'], encoding='utf-8') as f:
    print(list(f))  # → ['привет\n']

See also

Open in the full glossarydocs.python.orgReport an inaccuracy