python 2.3 for windowsで、パスに複数の引数とスペースを使用してghostscriptなどのプログラムを実行する最良の方法は何ですか?
-
03-07-2019 - |
質問
確かに、これを可能にする何らかの抽象化がありますか?
これは基本的にコマンドです
cmd = self._ghostscriptPath + 'gswin32c -q -dNOPAUSE -dBATCH -sDEVICE=tiffg4
-r196X204 -sPAPERSIZE=a4 -sOutputFile="' + tifDest + " " + pdfSource + '"'
os.popen(cmd)
この方法は私には本当に汚いように見えますが、いくつかのPython的な方法が必要です
解決
サブプロセスを使用し、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