Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 子流程-Grep断管_Python_Grep_Subprocess - Fatal编程技术网

Python 子流程-Grep断管

Python 子流程-Grep断管,python,grep,subprocess,Python,Grep,Subprocess,这里是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, stdou

这里是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()
def运行命令(客户机、设备):
路径=“/smithy/%s/%s%”(客户,设备)
thefiles=glob.glob(路径+'/*.smithy.xml')
p1=subprocess.Popen([“grep”,“”]+thefiles,stdout=subprocess.PIPE)
p2=subprocess.Popen(['wc-l'],stdin=p1.stdout,stdout=subprocess.PIPE)
p1.标准输出关闭()
count=p2.communicate()[0]
p1.等等
我在屏幕上收到许多“grep:writing output:break pipe”错误

一定是我错过了一些简单的东西,我就是看不出来。有什么想法吗


提前感谢您。

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

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

这里的问题是,对于
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()
def运行命令(客户机、设备):
路径=“/smithy/%s/%s%”(客户,设备)
thefiles=glob.glob(路径+'/*.smithy.xml')
p1=subprocess.Popen([“grep”,“”]+thefiles,stdout=subprocess.PIPE)
p2=subprocess.Popen(['wc','-l'],stdin=p1.stdout,stdout=subprocess.PIPE)
p1.标准输出关闭()
count=p2.communicate()[0]
p1.等等

这里的问题是,对于
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()
def运行命令(客户机、设备):
路径=“/smithy/%s/%s%”(客户,设备)
thefiles=glob.glob(路径+'/*.smithy.xml')
p1=subprocess.Popen([“grep”,“”]+thefiles,stdout=subprocess.PIPE)
p2=subprocess.Popen(['wc','-l'],stdin=p1.stdout,stdout=subprocess.PIPE)
p1.标准输出关闭()
count=p2.communicate()[0]
p1.等等

在子流程周围有一些(非常好的)包装,它们将使您的生活更加轻松,比如和。这些看起来非常酷-不幸的是,我没有在2.4模块之外添加模块的环境中这里有一些(非常好的)围绕子流程的包装将使您的生活更加轻松,如和。这些看起来非常酷-不幸的是,我没有在一个可以在2.4模块之外添加模块的环境中查看文档:--对我来说似乎是正确的。查看文档:--对我来说似乎是正确的。