4

確かにこれを可能にするある種の抽象化がありますか?

これは本質的にコマンドです

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

os.popen(cmd)

この方法は私には本当に汚いように見えます、いくつかのpythonicな方法があるに違いありません

4

1 に答える 1

6

サブプロセスを使用します。これは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]

サブプロセスモジュールを持たないpython2.3しかない場合でも、os.popenを使用できます。

os.popen(' '.join([
    self._ghostscriptPath, 
   'gswin32c',
   '-q',
   '-dNOPAUSE',
   '-dBATCH',
   '-sDEVICE=tiffg4',
   '-r196X204',
   '-sPAPERSIZE=a4',
   '-sOutputFile="%s %s"' % (tifDest, pdfSource),
]))
于 2008-10-21T08:00:56.213 に答える