-4

我将如何更正此代码以使用 subprocess 模块替换 popen2?

popen3 = popen2.Popen3(cmd, capturestderr=True)     
rc = popen3.wait()
if os.WIFEXITED(rc):
    rc = os.WEXITSTATUS(rc) 
        if rc < 0:
            #""" Needed to make sure that catastrophic errors are not processed here, hence the rc check.
            #"""                                             
            if len(stderr) > 0:
                inserts= []
                inserts.append("Warnings occurred during run of %s" % self.__MODULE_NAME )
                inserts.append("Check conversion parameters.")
                #self.msgWrite( "98000001", inserts )

        if rc == 0:
    self.msgDebug("CompartService exited normally", "Exit code with signal: %s" % str(rc))
    #
4

1 回答 1

3

简单的,

import subprocess

# the process
proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)

# getting different parts of it.
stdout = proc.stdout.read()
stderr = proc.stderr.read()
rc = proc.wait()

具体到您的代码现在的样子:

from subprocess import Popen, PIPE

proc = Popen(cmd, stderr=PIPE)
rc = proc.wait()
# the rest of your code
于 2013-10-10T15:27:03.577 回答