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

如何在python中获取终端输出?,第1张

如何在python中获取终端输出

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


>>>

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

import subprocessimport tempfilewith tempfile.TemporaryFile() as tempf:    proc = subprocess.Popen(['echo', 'a', 'b'], stdout=tempf)    proc.wait()    tempf.seek(0)    print tempf.read()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5644603.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存