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

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

      <bdo id='JVh6h'></bdo><ul id='JVh6h'></ul>
    1. <tfoot id='JVh6h'></tfoot>

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

      以冲突的可执行文件/路径打开

      Popen with conflicting executable/path(以冲突的可执行文件/路径打开)
      <i id='YB2WU'><tr id='YB2WU'><dt id='YB2WU'><q id='YB2WU'><span id='YB2WU'><b id='YB2WU'><form id='YB2WU'><ins id='YB2WU'></ins><ul id='YB2WU'></ul><sub id='YB2WU'></sub></form><legend id='YB2WU'></legend><bdo id='YB2WU'><pre id='YB2WU'><center id='YB2WU'></center></pre></bdo></b><th id='YB2WU'></th></span></q></dt></tr></i><div id='YB2WU'><tfoot id='YB2WU'></tfoot><dl id='YB2WU'><fieldset id='YB2WU'></fieldset></dl></div>

        • <tfoot id='YB2WU'></tfoot>
        • <small id='YB2WU'></small><noframes id='YB2WU'>

                <tbody id='YB2WU'></tbody>
            • <legend id='YB2WU'><style id='YB2WU'><dir id='YB2WU'><q id='YB2WU'></q></dir></style></legend>

                <bdo id='YB2WU'></bdo><ul id='YB2WU'></ul>
              • 本文介绍了以冲突的可执行文件/路径打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想使用 Popen 从我的 Python 脚本中调用 ImageMagick 的转换"实用程序,如下所示:

                I'd like to call the "convert" utility from ImageMagick from my Python script using Popen, like so:

                Popen(["convert", input_path, "-flop", output_file_path])
                

                (上面的例子只是将图像水平反转)

                (The above example simply reverses the image horizontally)

                问题是当我在 Windows 中运行脚本时,它错误地调用了 Windows 附带的 convert.exe 实用程序将 FAT 分区转换为 NTFS!(位于Windowssystem32)

                The problem is that when I run the script in Windows, it mistakenly calls the convert.exe utility that ships with Windows to convert FAT partitions to NTFS! (located in Windowssystem32)

                现在,如果我在 system32 以外的任何目录中随机打开命令提示符并键入convert",它会正确运行 ImageMagick 可执行文件.因此,这意味着 Popen 会自动在 system32 中查找.如何让它不在 system32 中查找,并运行正确的可执行文件?

                Now, if I randomly open a command prompt at any directory other than system32, and type "convert", it correctly runs the ImageMagick executable. So, this implies that Popen is automatically looking in system32. How can I make it not look in system32, and run the correct executable?

                推荐答案

                搜索一个程序并非易事.我会明确指定 convert.exe 可执行文件的完整路径.

                Search for a program is not trivial. I'd specify the full path to the convert.exe executable explicitly instead.

                subprocess 使用 CreateProcess 在 system32 目录中查找,甚至在 %PATH% 中的任何其他目录之前:

                subprocess uses CreateProcess on Windows that looks in system32 directory even before any other directory in %PATH%:

                ...如果文件名不包含扩展名,则附加.exe.因此,如果文件扩展名为.com,则该参数必须包括 .com 扩展名.如果文件名以句点 (.) 结尾没有扩展名,或者如果文件名包含路径,则 .exe 不是附加.如果文件名不包含目录路径,则系统按以下顺序搜索可执行文件:

                ... If the file name does not contain an extension, .exe is appended. Therefore, if the file name extension is .com, this parameter must include the .com extension. If the file name ends in a period (.) with no extension, or if the file name contains a path, .exe is not appended. If the file name does not contain a directory path, the system searches for the executable file in the following sequence:

                1. 加载应用程序的目录.
                2. 父进程的当前目录.
                3. 32 位 Windows 系统目录.使用 GetSystemDirectory 函数获取该目录的路径.
                4. 16 位 Windows 系统目录.没有函数获取这个目录的路径,但是被搜索了.此目录的名称是 System.
                5. Windows 目录.使用 GetWindowsDirectory 函数获取该目录的路径.
                6. PATH 环境变量中列出的目录.请注意,此函数不会搜索 App Paths 注册表项指定的每个应用程序路径.要在搜索序列中包含此每个应用程序路径,请使用 ShellExecute 函数.

                因此 convert 在这种情况下等同于 convert.exe.它首先查找包含 sys.executable 的目录,例如 C:Python27.然后在当前目录中:您启动 Python 脚本的位置.然后在 system32 中找到 convert.exe (文件系统实用程序,而不是 imagemagick).

                Therefore convert is equivalent to convert.exe in this case. It first looks in a directory that contains sys.executable e.g., C:Python27. Then in the current directory: where you started the Python script from. Then in system32 where it finds convert.exe (filesystem utility, not imagemagick).

                您可以尝试从 os.environ['PATH'] 中删除 system32 目录,它可能(?)禁止检查它:Popen(cmd, env=no_system32_environ)但它很脆弱(比显式路径更糟糕).

                You could try to remove system32 directory from os.environ['PATH'] it may(?) suppress checking it: Popen(cmd, env=no_system32_environ) but it is fragile (worse than the explicit path).

                在 Python 错误跟踪器上有一个相关问题:"子进程在 Windows 上选择了错误的可执行文件."

                There is a related issue on Python bug tracker: "Subprocess picks the wrong executable on Windows."

                cmd.exe(shell)使用不同的算法.请参阅Windows 如何定位在 shell 中输入的文件?

                cmd.exe (the shell) uses different algorithm. See How does Windows locate files input in the shell?

                如果你设置 shell=True 那么 convert 程序的搜索顺序:

                If you set shell=True then the search sequence for convert program:

                1. convert 不是内部 shell 命令
                2. 没有明确的路径,所以继续搜索
                3. 搜索当前目录
                4. 按列出的顺序搜索 PATH 环境变量指定的每个目录
                1. convert is not an internal shell command
                2. there is no explicit path, so the search continues
                3. search the current directory
                4. search each directory specified by the PATH environment variable, in the order listed

                %PATHEXT% 定义检查哪些文件扩展名以及检查顺序,例如 convert.com、convert.exe、convert.bat、convert.cmd if %PATHEXT%.com;.exe;.bat;.cmd.

                %PATHEXT% defines which file extensions are checked and in what order e.g., convert.com, convert.exe, convert.bat, convert.cmd if %PATHEXT% is .com;.exe;.bat;.cmd.

                这篇关于以冲突的可执行文件/路径打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 画布滚动条?)

                  1. <tfoot id='3cO0u'></tfoot>
                      <tbody id='3cO0u'></tbody>
                    • <bdo id='3cO0u'></bdo><ul id='3cO0u'></ul>
                      <legend id='3cO0u'><style id='3cO0u'><dir id='3cO0u'><q id='3cO0u'></q></dir></style></legend>

                      <small id='3cO0u'></small><noframes id='3cO0u'>

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