我希望将一个命令的输出传递给另一个命令。命令1必须在命令1执行完成后运行,依此类推。我尝试使用'subprocess.Popen()'的'wait'。但不知怎的,它不起作用。
有人可以帮我这个吗?
使用的代码:
proc= subprocess.Popen(['python27','scriptA.py',file],cwd="C:\pythonPrograms\\",stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
proc.wait()
sortedop= subprocess.Popen(['sort'],cwd="C:\pythonPrograms\\",stdout=subprocess.PIPE,stdin=proc.stdout, stderr=subprocess.STDOUT)
sortedop.wait()
countReducer= subprocess.Popen(['python27', 'scriptB.py'],cwd="C:\pythonPrograms\\",stdout=subprocess.PIPE,stdin=sortedop.stdout, stderr=subprocess.STDOUT)
countReducer.wait()
fd = open(file+".json", 'w')
countpostprocesser= subprocess.Popen(['python27', 'scriptC.py'],cwd="C:\pythonPrograms\\",stdout=fd,stdin=countReducer.stdout,stderr=subprocess.STDOUT,shell=True)
countpostprocesser.wait()
fd.close()
答案 0 :(得分:1)
如果你真的希望在下一个脚本启动之前完成一个脚本并且输出数据不是太大,你可以将数据读入内存:
proc= subprocess.Popen(['python27','scriptA.py',file],cwd="C:\\pythonPrograms\\",stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
sortedop= subprocess.Popen(['sort'],cwd="C:\pythonPrograms\\",stdout=subprocess.PIPE,stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
out, err = sortedop.communicate(out)
countReducer= subprocess.Popen(['python27', 'scriptB.py'],cwd="C:\pythonPrograms\\",stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
out, err = countReducer.communicate(out)
fd = open(file+".json", 'w')
countpostprocesser= subprocess.Popen(['python27', 'scriptC.py'],cwd="C:\pythonPrograms\\",stdout=fd, stderr=subprocess.STDOUT)
out, err = countpostprocesser.communicate(out)