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

如何在python中获取终端输出?

如何在python中获取终端输出?

import subprocess >>> cmd = [ ‘echo’, ‘arg1’, ‘arg2’ ] >>> output = subprocess.Popen( cmd, stdout=subprocess.PIPE ).communicate()[0] >>> print output arg1 arg2

>>>

使用subprocess.PIPE时存在错误。对于巨大的输出,请使用以下命令:

import subprocess
import tempfile

with tempfile.TemporaryFile() as tempf:
    proc = subprocess.Popen(['echo', 'a', 'b'], stdout=tempf)
    proc.wait()
    tempf.seek(0)
    print tempf.read()
python 2022/1/1 18:31:31 有196人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

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

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

请先登录

推荐问题


联系我
置顶