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'

 

python os.system重定向stdout到变量 ,同时获取返回值的更多相关文章

  1. python 利用python的subprocess模块执行外部命令,获取返回值

    有时执行dos命令需要保存返回值 需要导入库subprocess import subprocess p = subprocess.Popen('ping www.baidu.com', shell= ...

  2. python中subprocess.Popen执行命令并持续获取返回值

    先举一个Android查询连接设备的命令来看看Python中subprocess.Popen怎么样的写法.用到的命令为 adb devices. import subprocess order='ad ...

  3. python执行系统命令后获取返回值

    import os, subprocess # os.system('dir') #执行系统命令,没有获取返回值,windows下中文乱码 # result = os.popen('dir') #执行 ...

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

    python执行系统命令后获取返回值的几种方式集合 今天小编就为大家分享一篇python执行系统命令后获取返回值的几种方式集合,具有很好的参考价值,希望对大家有所帮助.一起跟随小编过来看看吧 第一种情 ...

  5. python os.system()返回值判断

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

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

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

  7. python os.system 和popen

    1.python  os.system 和popen  其中第一个只会返回0或者1,另外一个会返回执行结果 每天生成一个文件,并把磁盘的使用情况写到到这个文件中,文件名为日期格式(yyyy-mm-dd ...

  8. (转)[SQL Server] 动态sql给变量赋值(或返回值给变量)

    本文转载自:http://blog.csdn.net/xiaoxu0123/article/details/5684680 [SQL Server] 动态sql给变量赋值(或返回值给变量) decla ...

  9. execute sp_executesql 用变量获取返回值

    execute sp_executesql 用变量获取返回值 1,EXEC的使用 2,sp_executesql的使用 MSSQL为我们提供了两种动态执行SQL语句的命令,分别是EXEC和sp_exe ...

随机推荐

  1. 【西祠日志】【07】努力努力,找资料,思考,怎么做asp图片上传

    [西祠日志][07]努力努力,找资料.思考.怎么做asp图片上传  (2015.07.23周四) 今天忘了带本子.直接写在书上了笔记,晚点还是夹在本子里. 学了这么久的web应用,一直都没时间去做一点 ...

  2. mysqldump导入导出详解

    mysqldump可以指定路径的,如果没指定路径,而只写了文件名的话,那么就在当前进入mysql命令行所在的目录,也就是mysql安装目录下 mysqldump  --default-characte ...

  3. C#中??和?分别是什么意思? 在ASP.NET开发中一些单词的标准缩写 C#SESSION丢失问题的解决办法 在C#中INTERFACE与ABSTRACT CLASS的区别 SQL命令语句小技巧 JQUERY判断CHECKBOX是否选中三种方法 JS中!=、==、!==、===的用法和区别 在对象比较中,对象相等和对象一致分别指的是什么?

    C#中??和?分别是什么意思? 在C#中??和?分别是什么意思? 1. 可空类型修饰符(?):引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空.例如:string str=null; ...

  4. How to reset your password in Ubuntu

    There are many reasons you might want to reset a password: Someone gave you a computer with Ubuntu i ...

  5. Terminal emulator

    http://en.wikipedia.org/wiki/Terminal_emulator Terminal emulator From Wikipedia, the free encycloped ...

  6. NAT和桥接的区别

    NAT 桥接 NAT相当于是局域网中的局域网,把192.168.21.1当作外网ip,重新划分了一个网关(192.168.33.x) 网桥只是把网络桥接起来,还是原来的网关(192.168.21.x) ...

  7. Laravel建站04--建立后台文章管理

    路由配置 Route::group(['middleware' => 'auth', 'namespace' => 'Admin', 'prefix' => 'admin'], fu ...

  8. Struts2使用POI创建Excel并下载

    本文将讲解在Struts2框架下如何使用POI创建Office Excel文档并实现下载功能. Apache POI ,操作微软文档的Java API,简单来说就是可以用来操作Office文档的API ...

  9. c# CacheManager 缓存管理

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. android android:duplicateParentState="true" "false"

    今天要做一个效果.组件RelativeLayout上有两个TextView.这两个TextView具有不同的颜色值,如今要的效果是,当RelativeLayout被点击时,整个item有高亮背景. 同 ...