Windows上的python 2.3最好的方法是执行具有多个参数和路径中空格的ghostscript之类的程序吗? [英] What is the best way on python 2.3 for windows to execute a program like ghostscript with multiple arguments and spaces in paths?

查看:97
本文介绍了Windows上的python 2.3最好的方法是执行具有多个参数和路径中空格的ghostscript之类的程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

肯定有某种抽象允许这种情况吗?

Surely there is some kind of abstraction that allows for this?

这本质上是命令

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

os.popen(cmd)

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

this way looks really dirty to me, there must be some pythonic way

推荐答案

使用子流程,取代了os.popen,尽管它并不过是一种抽象:

Use subprocess, it superseeds os.popen, though it is not much more of an abstraction:

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

If you have only python 2.3 which has no subprocess module, you can still use 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最好的方法是执行具有多个参数和路径中空格的ghostscript之类的程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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