0
我试图编写一个函数来创建一个循环中的shell管道,它从列表中获取其命令参数并将最后一个stdout传递给新的stdin。 在命令列表中,我想调用Popen对象的通信方法来获取输出。python popen pipe in loop
输出总是空的。我究竟做错了什么?
见下面的例子:
lstCmd = ["tasklist", "grep %SESSIONNAME%", "grep %s" % (strAutName)]
lstPopen = [subprocess.Popen(lstCmd[0].split(), stdout=subprocess.PIPE)]
for i in range(len(lstCmd) - 1):
lstPopen.append(subprocess.Popen(lstCmd[i + 1].split(), stdin=lstPopen[i].stdout, stdout=subprocess.PIPE))
lstPopen[i].stdout.close()
strProcessInfo = lstPopen[-1].communicate()[0]
我在Windows环境下的其他UNIX的功能。以下命令适用于我的Windows命令行,并应写入strProcessInfo:
C:\>tasklist | grep %SESSIONNAME% | grep tasklist
tasklist.exe 18112 Console 1 5.948 K
我刚刚** ** os.environ [“SESSIONNAME”])**取代**控制台**,但它工作正常。谢谢! –