什么是python 2.3 for windows执行像ghostscript这样的程序在路径中有多个参数和空格的最佳方法?
-
03-07-2019 - |
题
当然有某种抽象可以实现这个目标吗?
这基本上就是命令
cmd = self._ghostscriptPath + 'gswin32c -q -dNOPAUSE -dBATCH -sDEVICE=tiffg4
-r196X204 -sPAPERSIZE=a4 -sOutputFile="' + tifDest + " " + pdfSource + '"'
os.popen(cmd)
这种方式对我来说看起来很脏,必须有一些pythonic方式
解决方案
使用子流程,它取代了os.popen,尽管它不仅仅是一个抽象概念:
from subprocess import Popen, PIPE
output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]
#this is how I'd mangle the arguments together
output = Popen([
self._ghostscriptPath,
'gswin32c',
'-q',
'-dNOPAUSE',
'-dBATCH',
'-sDEVICE=tiffg4',
'-r196X204',
'-sPAPERSIZE=a4',
'-sOutputFile="%s %s"' % (tifDest, pdfSource),
], stdout=PIPE).communicate()[0]
如果你只有没有子进程模块的python 2.3,你仍然可以使用os.popen
os.popen(' '.join([
self._ghostscriptPath,
'gswin32c',
'-q',
'-dNOPAUSE',
'-dBATCH',
'-sDEVICE=tiffg4',
'-r196X204',
'-sPAPERSIZE=a4',
'-sOutputFile="%s %s"' % (tifDest, pdfSource),
]))
不隶属于 StackOverflow