Python中的子进程挂起
我正在尝试使用Python和子流程从Perl脚本中检索一些信息:Python中的子进程挂起,python,perl,command,buffer,subprocess,Python,Perl,Command,Buffer,Subprocess,我正在尝试使用Python和子流程从Perl脚本中检索一些信息: command = ["perl","script.perl","arg1.txt","<","arg2.txt"] print " ".join(command) p = subprocess.Popen(command,stdout=subprocess.PIPE,shell=True) text = p.stdout.read() command=[“perl”、“script.perl”、“arg1.txt”、“如
command = ["perl","script.perl","arg1.txt","<","arg2.txt"]
print " ".join(command)
p = subprocess.Popen(command,stdout=subprocess.PIPE,shell=True)
text = p.stdout.read()
command=[“perl”、“script.perl”、“arg1.txt”、“如果您只需要一个简单的输入重定向,就不需要使用shell。用Python打开文件,并通过stdin
参数将文件句柄传递给Popen
with open("arg2.txt") as infile:
command = ["perl", "script.perl", "arg1.txt"]
p = subprocess.Popen(command, stdout=subprocess.PIPE, stdin=infile)
text = p.stdout.read()
或
它会阻塞,好像它仍在尝试读取继承的标准输入
subprocess.call("cat < .bashrc".split(), shell=True, stdin=open("/dev/null"))
subprocess.call(“cat<.bashrc.split(),shell=True,stdin=open(“/dev/null”))
调用立即返回。在这两种情况下,cat
似乎忽略了它的进一步参数。您可以尝试使用strace-ff
运行Python,以准确发现是什么操作使它挂起。
subprocess.call("cat < .bashrc".split(), shell=True)
subprocess.call("cat < .bashrc".split(), shell=True, stdin=open("/dev/null"))