gpt4 book ai didi

windows - 在 Python 的子进程中使用 Windows 路径(指向可执行文件)

转载 作者:可可西里 更新时间:2023-11-01 10:03:51 27 4
gpt4 key购买 nike

我开始在安装了 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)

在我的 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

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

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

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)

我已阅读:

没用

总是报错:

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

我完全理解这是菜鸟的东西 + 切换到 unix 会让我的生活更轻松 - 但在我的公司环境中我没有完全的选择。

我也试过:

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

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

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

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

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

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

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

它在 Windows 系统上的工作如此简单仍然让我感到困扰,仅仅因为它安装了 cygwin,以某种方式修改了 cmd 命名空间以识别“gs”...

最佳答案

路径在 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']) 

关于windows - 在 Python 的子进程中使用 Windows 路径(指向可执行文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41186323/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com