2008-10-21 4 views
3

분명히 이것을 허용하는 추상화가 있습니까? 그것은 훨씬 더 아니지만파이썬 2.3에서 패스가 여러 인자와 공백을 가진 ghostscript와 같은 프로그램을 실행하는 가장 좋은 방법은 무엇입니까?

이 기본적으로 명령이 방법은 나에게 정말 더러운 보이는

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

os.popen(cmd) 
이다,

답변

5

사용 subprocess, 그것은 일은 os.popen superseeds 일부 파이썬 방법이 있어야합니다 추상화 : 당신은 어떤 서브 프로세스 모듈이없는 2.3 파이썬 경우

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] 

, 당신은 여전히 ​​

을 일은 os.popen 사용할 수 있습니다
os.popen(' '.join([ 
    self._ghostscriptPath, 
    'gswin32c', 
    '-q', 
    '-dNOPAUSE', 
    '-dBATCH', 
    '-sDEVICE=tiffg4', 
    '-r196X204', 
    '-sPAPERSIZE=a4', 
    '-sOutputFile="%s %s"' % (tifDest, pdfSource), 
]))