子流程 - Grep Broken Pipe

Cha*_*ter 1 python grep subprocess

Python 2.4.x在这里.

一直在试图让subprocess与glob一起工作.

好吧,这是问题所在.

def runCommands(thecust, thedevice):
    thepath='/smithy/%s/%s' % (thecust,thedevice)
    thefiles=glob.glob(thepath + '/*.smithy.xml')
    p1=subprocess.Popen(["grep", "<record>"] + thefiles, stdout=subprocess.PIPE)
    p2=subprocess.Popen(['wc -l'], stdin=p1.stdout, stdout=subprocess.PIPE)
    p1.stdout.close()
    thecount=p2.communicate()[0]
    p1.wait()
Run Code Online (Sandbox Code Playgroud)

我在屏幕上收到许多"grep:写输出:断管"错误.

它必须是我想念的简单事物,我无法发现它.任何的想法?

先感谢您.

And*_*ark 5

这里的问题是p2你的参数列表应该['wc', '-l']代替['wc -l'].

目前它正在寻找一个名为'wc -l'run而不能找到它的可执行文件,因此p2立即失败并且没有任何连接p1.stdout,这会导致管道错误.

请尝试以下代码:

def runCommands(thecust, thedevice):
    thepath='/smithy/%s/%s' % (thecust,thedevice)
    thefiles=glob.glob(thepath + '/*.smithy.xml')
    p1=subprocess.Popen(["grep", "<record>"] + thefiles, stdout=subprocess.PIPE)
    p2=subprocess.Popen(['wc', '-l'], stdin=p1.stdout, stdout=subprocess.PIPE)
    p1.stdout.close()
    thecount=p2.communicate()[0]
    p1.wait()
Run Code Online (Sandbox Code Playgroud)