Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.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子流程中多次使用cut命令?_Python_Bash_Ubuntu - Fatal编程技术网

如何在Python子流程中多次使用cut命令?

如何在Python子流程中多次使用cut命令?,python,bash,ubuntu,Python,Bash,Ubuntu,我尝试在如下子流程中使用cut命令: subprocess.Popen(['cut', '-d', '''(''', '-f2', 'file1.txt', '|', 'cut', '-d', ''')''', '-f1']) 并获取以下错误: cut: only one type of list may be specified 如何更正它?通过组合多个Popen对象,自己构建管道: p1 = subprocess.Popen(['cut', '-d(', '-f2', 'file1.tx

我尝试在如下子流程中使用cut命令:

subprocess.Popen(['cut', '-d', '''(''', '-f2', 'file1.txt', '|', 'cut', '-d', ''')''', '-f1'])
并获取以下错误:

cut: only one type of list may be specified

如何更正它?

通过组合多个
Popen
对象,自己构建管道:

p1 = subprocess.Popen(['cut', '-d(', '-f2', 'file1.txt'], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['cut', '-d)', '-f1'], stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close()
print p2.communicate()[0]

使用带有参数列表的
popen
,并且
shell=False
表示没有shell。没有外壳,就不能有外壳管道……你可以用多个
Popen
对象串在一起建立自己的管道,这是你应该做的;阅读模块文档。我想这一切都可以很容易地完成,而不需要子流程,这比我(删除的)尝试要清楚得多。谢谢