Python 3.11+localefunction
locale.getencoding()
encoding
Returns the encoding of the current locale (LC_CTYPE), ignoring Python's UTF-8 mode.
Syntax
locale.getencoding()
Unlike getpreferredencoding(False), UTF-8 mode does not override it: on a Russian Windows it returns 'cp1251' even while open() already writes UTF-8. That matters since 3.15, where UTF-8 mode is on by default: getencoding() reports the system encoding, not the one Python opens files with. It arrived in 3.11 as the replacement for older ways of finding the encoding.
Examples
import locale
import sys
print(locale.getencoding()) # → ? UTF-8 или cp1251 — кодировка системы
print(locale.getpreferredencoding(False)) # → ? utf-8 в режиме UTF-8
print(sys.flags.utf8_mode) # → ? 1 — по умолчанию с 3.15
See also
Open in the full glossarydocs.python.orgReport an inaccuracy