您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

运行包含管道的命令行并将结果显示到STDOUT

运行包含管道的命令行并将结果显示到STDOUT

使用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()

或者,使用sh模块,管道成为功能的组合

import sh
output = sh.tail(sh.cat('file.log'), '-1')
其他 2022/1/1 18:34:52 有449人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶