在 Python 的子进程中使用 Windows 路径(指向可执行文件)[初学者]

Mar*_*Ton 3 windows executable subprocess cmd python-2.7

我开始在安装了 cygwin(Python 2.7)的 Windows 7 x64 机器上开发一个小的 pdf 到 jpg 脚本。以下工作完美:

import subprocess
filename = "test"
subprocess.check_output('gs -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT)
Run Code Online (Sandbox Code Playgroud)

在我的 Windows 10 x64 非 Cygwin 机器(Python 2.7)上拉取此项目后,此代码错误,因为它不再将“gs”识别为 ghostscript 的内置缩写。所以我尝试以下操作:

import subprocess
filename = "test"
subprocess.check_output('C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT)

WindowsError: [Error 2] The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)

好吧,我是初学者,在这里犯了一些明显的错误。"\b"中间的字符也.20\bin\被突出显示......向我表明我不知何故没有明确这是一条单一的路径。

我也尝试过的事情(单独运行):

subprocess.check_output(r'C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT')

subprocess.check_output("C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT/s", shell=True)
Run Code Online (Sandbox Code Playgroud)

我已阅读: *如何在 Python 中使用 dir/s 命令? *如何从 python 执行命令提示符命令 *用子进程包装 cmd.exe * Python 子进程不采用正确的参数 *用子进程包装 cmd.exe无济于事:(。

给出的错误总是:

Traceback (most recent call last):
File "C:/Python/Project/projectx/manual_conversion/manual_conversion.py", line 16, in <module>
subprocess.check_output(r'C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT)
File "C:\Python\Python 2.7.12\lib\subprocess.py", line 567, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "C:\Python\Python 2.7.12\lib\subprocess.py", line 711, in __init__
errread, errwrite)
File "C:\Python\Python 2.7.12\lib\subprocess.py", line 959, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)

我完全理解这是新手的东西 + 切换到 unix 会让我的生活更轻松 - 但在我的公司环境中,我并没有完全有那个选项 atm。任何帮助,将不胜感激!

第一个问题,如果需要,请指出任何更改!

编辑1:我也试过:

subprocess.check_output(r'C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o 2800-%03d.jpg test.pdf')
Run Code Online (Sandbox Code Playgroud)

因为我认为我的变量连接可能会破坏命令......但这再次产生相同的错误。

编辑 2:将完整参数作为列表传递

subprocess.check_output([r'C:\Program Files\gs\gs9.20\bin\gswin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o 2800-%03d.jpg test.pdf'])
Run Code Online (Sandbox Code Playgroud)

产生相同的错误。但是这个:

subprocess.check_output([r'C:\Program Files\gs\gs9.20\bin\gswin64c.exe'])
Run Code Online (Sandbox Code Playgroud)

在 Windows 中启动 ghostscript。谢谢@lit 让我尝试一些东西,现在新的问题是如何将选项传递给subprocess'check_output 可执行文件:)。

仍然困扰着我,它在 Windows 系统上工作是多么容易,因为它安装了 cygwin,以某种方式修改了 cmd 命名空间以识别“gs”......

Mik*_*ler 5

路径在此处包含一个空格Program Files。通常,空格表示路径的终点。"在路径周围使用引号告诉操作系统不要将空格视为路径的结尾,而是将引号之间的所有文本作为路径。所以只需添加引号:

subprocess.check_output([r'"C:\Program Files\gs\gs9.20\bin\gswin64c.exe" -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o 2800-%03d.jpg test.pdf']) 
Run Code Online (Sandbox Code Playgroud)