Unicode 文件名到 python subprocess.call()

Unicode filename to python subprocess.call()(Unicode 文件名到 python subprocess.call())
本文介绍了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.

希望对你有帮助.

这篇关于Unicode 文件名到 python subprocess.call()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)