【发布时间】:2014-07-22 15:32:37
【问题描述】:
我想在 python 中执行一些 shell 命令。我有一个 main.py,它调用连续函数,我发现其中一些更容易在 shell 中完成。问题:我想自动完成所有这些!
我想做这样的代码:
sort fileIn | uniq > fileOut
我的问题是用管道字符来做。我试试:
from subprocess import call
call(['sort ',FileOut,'|',' uniq '])
或
p1 = subprocess.Popen(['sort ', FileOut], stdout=subprocess.PIPE)
p2 = subprocess.Popen([" wc","-l"], stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output,err = p2.communicate()
但这一切都不起作用。 (注意:FileOut 是一个字符串)
【问题讨论】: