1

Python 2.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:写入输出:断管”错误。

这一定是我想念的一些简单的东西,我只是无法发现它。任何想法?

先感谢您。

4

2 回答 2

5

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

目前它正在寻找一个名为'wc -l'运行的可执行文件但没有找到它,因此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()
于 2012-05-31T18:57:01.240 回答
0

这似乎是因为您p1.stdout在 grep 完成输出之前关闭。也许你的意思是关闭pt.stdin?不过,似乎没有任何理由关闭其中任何一个,所以我将删除该p1.stdout.close()声明。

于 2012-05-31T18:46:28.363 回答