<i id='F9Qgs'><tr id='F9Qgs'><dt id='F9Qgs'><q id='F9Qgs'><span id='F9Qgs'><b id='F9Qgs'><form id='F9Qgs'><ins id='F9Qgs'></ins><ul id='F9Qgs'></ul><sub id='F9Qgs'></sub></form><legend id='F9Qgs'></legend><bdo id='F9Qgs'><pre id='F9Qgs'><center id='F9Qgs'></center></pre></bdo></b><th id='F9Qgs'></th></span></q></dt></tr></i><div id='F9Qgs'><tfoot id='F9Qgs'></tfoot><dl id='F9Qgs'><fieldset id='F9Qgs'></fieldset></dl></div>
  1. <tfoot id='F9Qgs'></tfoot>

      <bdo id='F9Qgs'></bdo><ul id='F9Qgs'></ul>

      <legend id='F9Qgs'><style id='F9Qgs'><dir id='F9Qgs'><q id='F9Qgs'></q></dir></style></legend>

      <small id='F9Qgs'></small><noframes id='F9Qgs'>

    1. Unicode 文件名到 python subprocess.call()

      Unicode filename to python subprocess.call()(Unicode 文件名到 python subprocess.call())

            <tbody id='CTZZ1'></tbody>

              <bdo id='CTZZ1'></bdo><ul id='CTZZ1'></ul>

              <legend id='CTZZ1'><style id='CTZZ1'><dir id='CTZZ1'><q id='CTZZ1'></q></dir></style></legend>
              <tfoot id='CTZZ1'></tfoot>

              <small id='CTZZ1'></small><noframes id='CTZZ1'>

              <i id='CTZZ1'><tr id='CTZZ1'><dt id='CTZZ1'><q id='CTZZ1'><span id='CTZZ1'><b id='CTZZ1'><form id='CTZZ1'><ins id='CTZZ1'></ins><ul id='CTZZ1'></ul><sub id='CTZZ1'></sub></form><legend id='CTZZ1'></legend><bdo id='CTZZ1'><pre id='CTZZ1'><center id='CTZZ1'></center></pre></bdo></b><th id='CTZZ1'></th></span></q></dt></tr></i><div id='CTZZ1'><tfoot id='CTZZ1'></tfoot><dl id='CTZZ1'><fieldset id='CTZZ1'></fieldset></dl></div>
              • 本文介绍了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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)

                      <tfoot id='41fpc'></tfoot>

                        <tbody id='41fpc'></tbody>

                        <legend id='41fpc'><style id='41fpc'><dir id='41fpc'><q id='41fpc'></q></dir></style></legend>

                        <small id='41fpc'></small><noframes id='41fpc'>

                        <i id='41fpc'><tr id='41fpc'><dt id='41fpc'><q id='41fpc'><span id='41fpc'><b id='41fpc'><form id='41fpc'><ins id='41fpc'></ins><ul id='41fpc'></ul><sub id='41fpc'></sub></form><legend id='41fpc'></legend><bdo id='41fpc'><pre id='41fpc'><center id='41fpc'></center></pre></bdo></b><th id='41fpc'></th></span></q></dt></tr></i><div id='41fpc'><tfoot id='41fpc'></tfoot><dl id='41fpc'><fieldset id='41fpc'></fieldset></dl></div>

                          <bdo id='41fpc'></bdo><ul id='41fpc'></ul>