我正在执行一个模块popen1.py,它使用子进程模块调用popen2.py,
但是popen2.py的输出不是displayed..When,我显示子进程id,它的输出是displayed..Where。
打电话
child = subprocess.Popen(['python', 'popen2.py',"parm1='test'","parm='test1'"], shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)发布于 2012-10-23 22:57:43
在这个过程完成之后,您可以读取child.stdout和child.stderr来获取数据(因为您传递了subprocess.PIPE)
或者,您可以使用oudata,errdata = child.communicate(),它将等待子进程完成,然后将其输出为字符串。
从设计的角度来看,最好是导入。我将按如下方式重构popen2.py:
#popen2.py
# ... stuff here
def run(*argv):
#...
if __name__ == '__main__':
import sys
run(sys.argv[1:])然后你可以在popen1.py中导入并运行popen2.py:
#popen1.py
import popen2
popen2.run("parm1=test","parm=test1")发布于 2012-10-23 23:04:43
您可以使用can .stdout/can.stdin与该进程进行通信:
child = subprocess.Popen(['python', 'popen2.py',"parm1='test'","parm='test1'"], shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print child.stdout.readlines()https://stackoverflow.com/questions/13033215
复制相似问题