在Python的子进程中使用Windows路径(指向可执行文件)[初学者] [英] Using a Windows path within Python's subprocess (point to an executable) [beginner]

查看:109
本文介绍了在Python的子进程中使用Windows路径(指向可执行文件)[初学者]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在装有cygwin(Python 2.7)的Windows 7 x64计算机上开发一个小的pdf到jpg脚本。以下内容可以正常工作:

I started developing a small pdf to jpg script on a Windows 7 x64 machine with cygwin installed (Python 2.7). The following works perfectly:

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的缩写。因此,我尝试以下操作:

After pulling this project on my Windows 10 x64 non-Cygwin machine (Python 2.7), this code errors as it no longer recognizes "gs" as a built-in short for ghostscript. So I try the following:

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 字符也突出显示...我以某种方式并不清楚这是一条单一路径。

Ok fine, I am a beginner and making some obvious mistake here. Also the "\b" characters in between .20\bin\ are highlighted ... indicating to me I somehow haven't made clear this is a single path.

我也尝试过的方法(单独运行):

Things I have also tried (separate runs):

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)

我读过:
* 如何在Python中使用dir / s命令?
* 如何执行命令来自python的ompt命令
* 将cmd.exe与子进程包装
* Python子进程没有正确的参数
* 将cmd.exe与子进程包装无济于事:(。

I have read: * How to use the dir/s command in Python? * How to execute a command prompt command from python * wrapping cmd.exe with subprocess * Python subprocess does not take correct arguments * wrapping cmd.exe with subprocess to no avail :( .

始终给出的错误是:

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将使我的生活更轻松-但在我的公司环境中,我没有完全拥有该选项的atm。任何帮助将不胜感激!

I fully understand this is rookie stuff + switching to unix would make my life easier - but in my corporate environment I don't fully have that option atm. Any help would be appreciated!

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

编辑1:我也尝试过:

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

因为我以为我的变量串联可能会破坏命令……但这又会产生相同的错误。

Since I thought maybe my variable concatenation was breaking the command ... but this again yields the same error.

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

EDIT 2: Passing the full argument as a list

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

产生相同的错误。但这:

Yields the same error. But this:

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

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

Does start up ghostscript in Windows. Thank you @lit for making me try things, now the new problem is how to pass options to subprocess' check_output executable :) .

仍然让我感到困扰的是,它仅在cygwin上安装了cygwin,就以某种方式修改了cmd命名空间以识别 gs ...

Still bothers me how easy it worked on a Windows system simply because it had cygwin installed that somehow modified cmd namespace to recognize "gs"...

推荐答案

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

The path contains a space here Program Files. Typically, a space means end of the path. Using quotes " around the path tells the operating system not to consider the space as the end of the path but to take all the text in between the quotes as the path. So just add quotes:

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

这篇关于在Python的子进程中使用Windows路径(指向可执行文件)[初学者]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆