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

时间:2015-11-11 16:03:28

标签: python bash ubuntu

我正在尝试在子进程中使用cut命令:

subprocess.Popen(['cut', '-d', '''(''', '-f2', 'file1.txt', '|', 'cut', '-d', ''')''', '-f1'])

并收到此错误:

cut: only one type of list may be specified

我该如何纠正?

1 个答案:

答案 0 :(得分:3)

通过组合多个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]