What is the best way on python 2.3 for windows to execute a program like ghostscript with multiple arguments and spaces in paths?

后端 未结 1 1790
情歌与酒
情歌与酒 2021-01-06 09:45

Surely there is some kind of abstraction that allows for this?

This is essentially the command

cmd = self._ghostscriptPath + \'gswin32c -q -dNOPAUSE          


        
相关标签:
1条回答
  • 2021-01-06 10:05

    Use subprocess, it superseeds os.popen, though it is not much more of an abstraction:

    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]
    

    If you have only python 2.3 which has no subprocess module, you can still use os.popen

    os.popen(' '.join([
        self._ghostscriptPath, 
       'gswin32c',
       '-q',
       '-dNOPAUSE',
       '-dBATCH',
       '-sDEVICE=tiffg4',
       '-r196X204',
       '-sPAPERSIZE=a4',
       '-sOutputFile="%s %s"' % (tifDest, pdfSource),
    ]))
    
    0 讨论(0)
提交回复
热议问题