>>> import sys
>>> sys.getfilesystemencoding()
'UTF-8'
How do I change that? I know how to change the default system encoding.
>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding('ascii')
But there is no sys.setfilesystemencoding
.
There are two ways to change it:
export LC_CTYPE=en_US.UTF-8
before launching python:$ LC_CTYPE=C python -c 'import sys; print(sys.getfilesystemencoding())'
ANSI_X3.4-1968
$ LC_CTYPE=en_US.UTF-8 python -c 'import sys; print(sys.getfilesystemencoding())'
UTF-8
Note that LANG serves as the default value for LC_CTYPE if it is not set, while LC_ALL overrides both LC_CTYPE and LANG)
import sys
sys.getfilesystemencoding = lambda: 'UTF-8'
Both methods let functions like os.stat
accept unicode (python2.x) strings.
Otherwise those functions raise an exception when they see non-ascii symbols in the filename.
Update:
In the (1) variant the locale has to be available (present in locale -a
) for this setting to have the desired effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With