Windows在python 2.3上执行Windows程序的最佳方法是什么,例如在路径中带有多个参数和空格的ghostscript?

当然有某种抽象允许这种情况吗?这本质上是命令cmd = self._ghostscriptPath + gswin32c -q -dNOPAUSE -dBATCH -sDEVICE=tiffg4 -r196X204 -sPAPERSIZE=a4 -sOutputFile= + tifDest + + pdfSource + os.p...

当然有某种抽象允许这种情况吗?

这本质上是命令

cmd = self._ghostscriptPath + 'gswin32c -q -dNOPAUSE -dBATCH -sDEVICE=tiffg4 
      -r196X204 -sPAPERSIZE=a4 -sOutputFile="' + tifDest + " " + pdfSource + '"'

os.popen(cmd)

这种方式对我来说真的很脏,必须有一些pythonic方式

解决方法:

使用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而没有子进程模块,则仍然可以使用os.popen

os.popen(' '.join([
    self._ghostscriptPath, 
   'gswin32c',
   '-q',
   '-dNOPAUSE',
   '-dBATCH',
   '-sDEVICE=tiffg4',
   '-r196X204',
   '-sPAPERSIZE=a4',
   '-sOutputFile="%s %s"' % (tifDest, pdfSource),
]))

本文标题为:Windows在python 2.3上执行Windows程序的最佳方法是什么,例如在路径中带有多个参数和空格的ghostscript?

基础教程推荐