2015-03-02 61 views
0

我嘗試運行下面的代碼(在python3.4):如何用管道執行python Popen?

p1 = Popen(['screen', '-la'], stdout=PIPE) 
p2 = Popen(['tail', '-n', '+2'], stdin=p1.stdout, stdout=PIPE) 
p3 = Popen(['head', '-n', '-2'], stdin=p2.stdout, stdout=PIPE) 
p4 = Popen(['awk', '"{print $1}"'], stdin=p3.stdout, stdout=PIPE) 
p5 = Popen(['xargs', '-I{}', 'screen', '-S', '{}', '-X', 'quit'], stdin=p4.stdout, stdout=PIPE) 
p1.stdout.close() 
p2.stdout.close() 
p3.stdout.close() 
p4.stdout.close() 
out = p5.communicate[0] 
print(out) 

,我收到一個錯誤:

p5.communicate[0] 
TypeError: 'method' object is not subscriptable 

回答

1

「溝通」是一種方法,而不是一個屬性,你可以試試這個相反:

out = p5.communicate()[0] 
+0

啊......謝謝 – jonua 2015-03-02 10:34:53