python os.system重定向stdout到变量 ,同时获取返回值

Python执行系统命令的方法 os.system(),os.popen(),commands

最近在做那个测试框架的时候发现 Python 的另一个获得系统执行命令的返回值和输出的类。

最开始的时候用 Python 学会了 os.system() 这个方法是很多比如 C,Perl 相似的。

os.system('cat /proc/cpuinfo')

但是这样是无法获得到输出和返回值的,继续 Google,之后学会了 os.popen()。

output = os.popen('cat /proc/cpuinfo')
print output.read()

通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出。但是怎么读取程序执行的返回值呢,当然咯继续请教伟大的 Google(联想到像我这样的人工作如果离开了 Google,不是成了废物。。。Baidu 忽视)。Google 给我指向了 commands — Utilities for running commands。
这样通过 commands.getstatusoutput() 一个方法就可以获得到返回值和输出,非常好用。

(status, output) = commands.getstatusoutput('cat /proc/cpuinfo')
print status, output

Python Document 中给的一个例子,很清楚的给出了各方法的返回。

>>> import commands
>>> commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
>>> commands.getstatusoutput('cat /bin/junk')
(256, 'cat: /bin/junk: No such file or directory')
>>> commands.getstatusoutput('/bin/junk')
(256, 'sh: /bin/junk: not found')
>>> commands.getoutput('ls /bin/ls')
'/bin/ls'
>>> commands.getstatus('/bin/ls')
'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'

posted on 2017-02-21 09:44 秦瑞It行程实录 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/ruiy/p/6422586.html

python os.system重定向stdout到变量 ,同时获取返回值相关推荐

  1. python执行系统命令后获取返回值的几种方式集合

    第一种情况 os.system('ps aux') 执行系统命令,没有返回值 第二种情况 result = os.popen('ps aux') res = result.read() for lin ...

  2. python os system用法_Python调用系统命令os.system()和os.popen()的实现

    作为一门脚本语言,写脚本时执行系统命令可以说很常见了,python提供了相关的模块和方法. os模块提供了访问操作系统服务的功能,由于涉及到操作系统,它包含的内容比较多,这里只说system和pope ...

  3. python os.system返回值_python os.system()返回值判断

    最近遇到os.system()执行系统命令的情况,上网搜集了一下资料,整理如下,以备不时之需,同时也希望能帮到某些人. 一.python中的 os.system(cmd)的返回值与linux命令返回值 ...

  4. [Python陷阱]os.system调用shell脚本获取返回值

    当前有shell个脚本/tmp/test.sh,内容如下: #!/bin/bash exit 11 使用Python的os.system调用,获取返回值是: >>> ret=os.s ...

  5. 分享python os.system一点心得

    我记得在上次的文章中写到了python popen,用python popen处理一些系统的命令它会返回一个对象, 比如: import os print os.popen('ping g.cn') ...

  6. Python os.system(command),这样执行的command命令,和主程序是异步的吗?

    Python os.system(command),这样执行的command命令,和主程序是异步的吗? 是同步执行的. 尚未执行完成的情况下,下面的程序不会继续操作.看下面的例子即可: >> ...

  7. python os.system()和os.popen()

    1>python调用Shell脚本,有两种方法:os.system()和os.popen(), 前者返回值是脚本的 退出状态码,后者的返回值是脚本执行过程中的 输出内容. >>> ...

  8. python os.system保存返回值_python中os.system的返回值

    python中os.system()的返回值 1.python中的 os.system(cmd)的返回值与linux命令返回值(具体参见本文附加内容)的关系 你们都习惯用os.systemv()函数执 ...

  9. os.system获取返回值 python3 cmd 获取返回值

    os.system是无法获取返回值的 只返回一个cmd 可以使用 (status, uploadRes) = subprocess.getstatusoutput(cmd) import subpro ...

最新文章

  1. 网游生命周期在百度指数曲线上呈“M”形分布,各阶段搜索行为呈一定特征
  2. 利用 VMware vRealize - 构建和优化云管理
  3. uniapp 发布网站遇到的问题(跨域,nginx代理失败,index无法打开,手机端无法访问等)
  4. 解码器输出PSNR为0表示什么
  5. 2021-07-13
  6. 使用BackTrack4清除/修改Windows用户密码
  7. 系统集成项目管理工程师10大管理47个过程域输入输出工具(项目成本管理)
  8. 企业组织架构的架构图用思维导图软件怎么做?
  9. mysql 函数 人民币大写_sql 数字转人民币大写函数(两种方法)
  10. 帮蔡徐坤刷出一亿转发,APP开发者一审获刑
  11. 关于 iOS 的 StoryBoard,接受的那一刻才发现她的美 - 当然美的事物都需要业心照料
  12. P3435 [POI2006]OKR-Periods of Words [Kmp, next数组]
  13. 对dede标签调用的内容进行判断并输出的方法
  14. 5种小型设备上深度学习推理的高效算法
  15. 在java中使用openCV遍历红色,像素遍历(仅限java)
  16. Chrome 开发者工具 Initiator 面板单击后看不到 JavaScript 源代码的解决方法
  17. iOS 正确设置状态栏 Style
  18. 如何隐藏微信内置底部前进后退的按钮?
  19. pku1036Gangsters Dp
  20. 监控视频压缩2—Block-Composed Background Reference for High Efficiency Video Coding

热门文章

  1. python使用spark_apache spark - 在python中导入pyspark
  2. python登录程序编写-初学Python3 - 写一个登录程序
  3. php捕获500错误信息,php – 如何获取有关错误500的更多信息 – 内部服务器错误?...
  4. mapreduce 丢数据_大数据之MapReduce详解
  5. php如何一篇文章替换敏感字,php实现的替换敏感字符串类实例
  6. 2021年春季学期-信号与系统-第十次作业参考答案-第二小题
  7. 自制pyBoard的I2C实验,应用MicroPython
  8. 多股Litz线制作无线耦合线圈测试
  9. 粘贴铜箔高频实验板-简易电报发射机实验
  10. Python中将数据矢量化运算所带来的时间加快