Was ist der beste Weg, auf Python 2.3 für Windows ein Programm wie ghost mit mehreren Argumenten und Räumen in Bahnen ausgeführt werden?

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

  •  03-07-2019
  •  | 
  •  

Frage

Sicherlich gibt es eine Art von Abstraktion, die dies zulässt?

Dies ist im Wesentlichen der Befehl

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

os.popen(cmd)

auf diese Weise zu mir wirklich schmutzig aussieht, muss es einige pythonic Weg sein

War es hilfreich?

Lösung

Verwenden Sie subprocess , es superseeds os.popen, obwohl es nicht viel eher eine Abstraktion :

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]

Wenn Sie nur Python 2.3 haben, die keine subprocess Modul verfügt, können Sie immer noch os.popen verwenden

os.popen(' '.join([
    self._ghostscriptPath, 
   'gswin32c',
   '-q',
   '-dNOPAUSE',
   '-dBATCH',
   '-sDEVICE=tiffg4',
   '-r196X204',
   '-sPAPERSIZE=a4',
   '-sOutputFile="%s %s"' % (tifDest, pdfSource),
]))
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top