子流程Grep breaked Pip

2024-10-01 09:36:17 发布

您现在位置:Python中文网/ 问答频道 /正文

这里是python2.4.x。在

我一直在努力让子进程和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()

我在屏幕上收到许多“grep:writing output:breaked pipe”错误。在

一定是我错过了一些简单的东西,我就是找不到。有什么想法吗?在

提前谢谢你。在


Tags: 进程defstdoutgrepglobsubprocesspopenp2
2条回答

这里的问题是,对于p2,参数列表应该是['wc', '-l'],而不是{}。在

目前,它正在寻找名为'wc -l'的可执行文件来运行,但没有找到它,因此p2立即失败,并且没有与{}连接的任何内容,这将导致断管错误。在

请尝试以下代码:

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()

这似乎是因为在grep完成输出之前,p1.stdout正在关闭。也许你是想关闭pt.stdin?但是似乎没有任何理由关闭这两个语句,所以我只删除p1.stdout.close()语句。在

相关问题 更多 >