Unicode 文件名到 python subprocess.call()
问题描述
我正在尝试使用 unicode 文件名运行 subprocess.call(),这是简化的问题:
I'm trying to run subprocess.call() with unicode filename, and here is simplified problem:
n = u'c:\windows\notepad.exe '
f = u'c:\temp\nèw.txt'
subprocess.call(n + f)
这引发了著名的错误:
UnicodeEncodeError: 'ascii' 编解码器无法编码字符 u'xe8'
UnicodeEncodeError: 'ascii' codec can't encode character u'xe8'
编码为 utf-8 会产生错误的文件名,mbcs 将文件名作为 new.txt 传递,没有重音
Encoding to utf-8 produces wrong filename, and mbcs passes filename as new.txt without accent
对于这个令人困惑的主题,我实在看不下去了,只能绕圈子了.我在这里找到了很多关于过去许多不同问题的答案,所以我想自己加入并寻求帮助
I just can't read any more on this confusing subject and spin in circle. I found here lot of answers for many different problems in past so I thought to join and ask for help myself
谢谢
解决方案
我找到了一个很好的解决方法,它有点乱,但它有效.
I found a fine workaround, it's a bit messy, but it works.
subprocess.call 将以自己的编码将文本传递给终端,这可能是也可能不是它所期望的.因为你想让它可移植,所以你需要知道机器在运行时的编码.
subprocess.call is going to pass the text in its own encoding to the terminal, which might or not be the one it's expecting. Because you want to make it portable, you'll need to know the machine's encoding at runtime.
以下
notepad = 'C://Notepad.exe'
subprocess.call([notepad.encode(sys.getfilesystemencoding())])
尝试找出当前的编码,因此将正确的编码应用到 subprocess.call
attempts to figure out the current encoding and therefore applies the correct one to subprocess.call
作为旁注,我还发现,如果您尝试使用当前目录组合字符串,请使用
As a sidenote, I have also found that if you attempt to compose a string with the current directory, using
os.cwd()
Python(或操作系统,不知道)会用重音字符弄乱目录.为了防止这种情况,我发现以下方法可以工作:
Python (or the OS, don't know) will mess up directories with accented characters. To prevent this I have found the following to work:
os.cwd().decode(sys.getfilesystemencoding())
这与上面的解决方案非常相似.
Which is very similar to the solution above.
希望对你有帮助.
相关文章