控制台重定向输出到Python字符串 [英] Redirecting console output to a Python string
本文介绍了控制台重定向输出到Python字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
<一href=\"http://stackoverflow.com/questions/923079/how-can-i-capture-the-stdout-output-of-a-child-process\">How我可以捕获一个子进程的标准输出输出?
我运行一个猫
般在bash程序从Python的:
I'm running a cat
-like program in bash from Python:
import os
os.system('cat foo.txt')
我如何得到shell命令的输出回到Python脚本,是这样的:
How do I get the output of the shell command back in the Python script, something like:
s = somefunction('cat foo.txt')
UPD :<一href=\"http://stackoverflow.com/questions/125222/extracting-text-from-ms-word-files-in-python\">Here是一个相关话题。
推荐答案
使用子模块。
from subprocess import Popen, PIPE
(stdout, stderr) = Popen(["cat","foo.txt"], stdout=PIPE).communicate()
print stdout
这篇关于控制台重定向输出到Python字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文