在Windows下,Python 2.3执行带多个参数和路径空格的ghostscript程序的最佳方法是什么?
肯定有某种抽象的方法可以实现这个吧?
这基本上就是这个命令
cmd = self._ghostscriptPath + 'gswin32c -q -dNOPAUSE -dBATCH -sDEVICE=tiffg4
-r196X204 -sPAPERSIZE=a4 -sOutputFile="' + tifDest + " " + pdfSource + '"'
os.popen(cmd)
我觉得这种写法看起来真的很糟糕,一定有更符合Python风格的方法。
1 个回答
6
使用 subprocess 模块,它比 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 版本,没有 subprocess 模块,你仍然可以使用 os.popen。
os.popen(' '.join([
self._ghostscriptPath,
'gswin32c',
'-q',
'-dNOPAUSE',
'-dBATCH',
'-sDEVICE=tiffg4',
'-r196X204',
'-sPAPERSIZE=a4',
'-sOutputFile="%s %s"' % (tifDest, pdfSource),
]))