Unicode filenames on Windows with Python & subprocess.Popen()

前端 未结 5 1036
独厮守ぢ
独厮守ぢ 2020-11-27 08:04

Why does the following occur:

>>> u\'\\u0308\'.encode(\'mbcs\')   #UMLAUT
\'\\xa8\'
>>> u\'\\u041A\'.encode(\'mbcs\')   #CYRILLIC CAPITAL L         


        
5条回答
  •  清酒与你
    2020-11-27 08:39

    With Python 3, just don't encode the string. Windows filenames are natively Unicode, and all strings in Python 3 are Unicode, and Popen uses the Unicode version of the CreateProcess Windows API function.

    With Python 2.7, the easiest solution is to use the third-party module https://pypi.org/project/subprocessww/. There is no "built-in" solution to get full Unicode support (independent of system locale), and the maintainers of Python 2.7 consider this a feature request rather than a bugfix, so this is not going to change.

    For a detailed technical explanation of why things are as they are, please see the other answers.

提交回复
热议问题