python 2.3 for windowsで、パスに複数の引数とスペースを使用してghostscriptなどのプログラムを実行する最良の方法は何ですか?

StackOverflow https://stackoverflow.com/questions/221097

  •  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),
]))
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top