几天来,我一直遭受 UnicodeEncodeError 的困扰。
我搜索了一些文章,但它们都不起作用。
我试过这样
command = u'start C:\Windows\explorer.exe /select, "C:/한글.txt"'
subprocess.Popen(command, shell=True)
Traceback (most recent call last):
subprocess.Popen(command, shell=True)
File "C:\Python27\lib\subprocess.py", line 394, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 614, in _execute_child
args = '{} /c "{}"'.format (comspec, args)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 43-45: ordinal not in range(128)
我该如何解决这个问题?
我的解决方案
我被困在复杂的案件中。要运行 unicode 路径,我必须使用“ cp949 ”编码和os.realpath如下
path = os.path.realpath("C:/한글.txt") ## realpath
command = u'start C:\Windows\explorer.exe /select, "{}"'.format(path)
command = command.encode("cp949") ## encoding: cp949
subprocess.Popen(command, shell=True)