python 将命令行执行的屏幕输出赋值到out变量

来源:互联网 发布:python爬虫课程 编辑:程序博客网 时间:2024/05/06 19:11
1.捕获屏幕输出(因为后续要保存到log中)
2.得到执行shell程序的返回值


使用了subprocess来调用shell程序,
需要从屏幕看到工具的输出,并捕获这些输出(后续需要保存),
run_watch=subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
output,err_msg=run_watch.communicate()

run_watch.returncode

print output # 是打印捕获到的屏幕输出

print err_msg #打印err 信息

print run_watch.returncode 

# 打印返回值


0 0
原创粉丝点击