使用subprocess.PIPE,如子流程文档部分“替换外壳管道”中所述:
import subprocess
p1 = subprocess.Popen(["cat", "file.log"], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["tail", "-1"], stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output,err = p2.communicate()
import sh
output = sh.tail(sh.cat('file.log'), '-1')