How to change file system encoding via python?

后端 未结 2 1734
甜味超标
甜味超标 2020-12-19 03:36
>>> import sys
>>> sys.getfilesystemencoding()
\'UTF-8\'

How do I change that? I know how to change the default system encoding.<

2条回答
  •  遥遥无期
    2020-12-19 03:56

    There are two ways to change it:

    1) (linux-only) export LC_CTYPE=en_US.UTF8 before launching python:

    $ LC_CTYPE=C python -c 'import sys; print(sys.getfilesystemencoding())'
    ANSI_X3.4-1968
    $ LC_CTYPE=C.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)

    2) monkeypatching:

    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.

提交回复
热议问题