Python 2.3에서 Windows가 Paths에서 여러 인수와 공간이있는 Ghostscript와 같은 프로그램을 실행하는 가장 좋은 방법은 무엇입니까?
-
03-07-2019 - |
문제
분명히 이것을 허용하는 일종의 추상화가 있습니까?
이것은 본질적으로 명령입니다
cmd = self._ghostscriptPath + 'gswin32c -q -dNOPAUSE -dBATCH -sDEVICE=tiffg4
-r196X204 -sPAPERSIZE=a4 -sOutputFile="' + tifDest + " " + pdfSource + '"'
os.popen(cmd)
이런 식으로 나에게 정말 더럽게 보이고, 피스론 방식이 있어야합니다.
해결책
사용 하위 프로세스, 그것은 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