用子进程模块替换popen2模块

时间:2013-10-10 15:18:45

标签: python

如何更正此代码以使用子进程模块替换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))
    #

1 个答案:

答案 0 :(得分: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