python执行系统命令:os.system,os.popen,commands

写脚本的时候经常会直接执行系统命令。

一.最先使用的是os.system()命令。

import os
os.system("cat /etc/hosts")

但是吧,这个方法并不能取得输出和返回值的。

二.接着我就使用了os.popen()命令

import os
output = os.popen("cat /etc/hosts")
print output.read()

通过os.popen返回的是file read对象,因此要获取内容的话,直接可以output.read()操作看到输出结果。

这个方法也并不能取得返回值

三.commands命令

import commands
(status, output) = commands.getstatusoutput("cat /etc/hosts")
print type(status), status
print type(output), output

这里就可以取得返回状态跟返回值啦。

来看几个例子:(我用的ipython)

In [1]: import commands

In [3]: commands.getstatusoutput("ls /data/")
Out[3]: (0, ‘dictionary\nhotwords\nlogs\nwordsegment‘)

In [4]: commands.getstatusoutput("cat /etc/")
Out[4]: (256, ‘cat: /etc/: Is a directory‘)

In [6]: commands.getoutput("ls /etc/hosts")
Out[6]: ‘/etc/hosts‘

In [9]: commands.getstatus("/bin/ln")

Out[9]: ‘-rwxr-xr-x 1 root root 56072 Mar 24  2014 /bin/ln‘

时间: 2024-07-31 09:18:38

python执行系统命令:os.system,os.popen,commands的相关文章

[转] 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() 返

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

转载:http://blog.csdn.net/b_h_l/article/details/12654749 第一种:使用os.system() import osos.system('cat /etc/profile') 第二种:使用os.popen() import os output = os.popen('cat /proc/cpuinfo') print output.read() 通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以

windows linux 使用python执行系统命令并将结果保存到变量

最近需要用到os.system 发现不能赋值到变量 后查有更新的模块,如下: os.system os.spawn* os.popen* popen2.* commands.* 重新使用content=os.popen('help').read() 就能获取到了 import pyodbcimport sysimport osimport commands #python conn sql server2008R2conn = pyodbc.connect( driver='{sql serve

转 Python执行系统命令的方法

传送门 Python执行系统命令的方法 http://www.linux-field.com/?p=15 Python中执行系统命令常见方法有两种: 两者均需 import os (1) os.system # 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 system(command) -> exit_statusExecute the command (a string) in a subshell. # 如果再命令行下执行,结果直接打印出来 1 >>> os.

使用Python执行系统命令方法有哪些?

Python是一款操作简单的编程语言,内置丰富的库,能够很容易的实现强大的功能,在使用Python进行框架搭建时,往往需要用到Python执行系统命令,一些开发人员对此不熟悉,以下是具体的操作方法: 1. os.system() 这个方法直接调用标准C的system()函数,仅仅在一个子终端运行系统命令,而不能获取执行返回的信息. >>> import os >>> output = os.system('cat  /proc/cpuinfo') processor :

python 执行系统命令模块比较

python 执行系统命令模块比较 1.os.system模块 仅仅在子终端运行命令,返回状态码,0为成功,其他为失败,但是不返回执行结果 如果再命令行下执行,结果直接打印出来 >>> os.system('ls') 04101419778.CHM bash document media py-django video 11.wmv books downloads Pictures python all-20061022 Desktop Examples project tools 2.

[py] os.system os.popen commands 执行shell

? 1.仅输出到屏幕,pwd保存的是状态<=====可用于执行shell命令 pwd=os.system(pwd) ? 2.popen可以保存命令结果 pwd=os.popen('pwd').read() ? 3,返回状态和命令结果 pwd=commands.getstatusoutput('pwd') (0, '/home/py')

Python执行系统命令的方法

Python中执行系统命令常见方法有两种: 两者均需 import os (1) os.system # 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 system(command) -> exit_statusExecute the command (a string) in a subshell. # 如果再命令行下执行,结果直接打印出来 1 >>> os.system('ls') 2 04101419778.CHM   bash      document  

python执行系统命令的四种方式

一.os模块 1. os.system('cmd') 在子终端运行系统命令,不能获取命令执行后的返回信息以及执行返回的状态 import os os.system('date') # 2016年 06月 30日 星期四 19:26:21 CST OS.system 2. os.popen(cmd) 不仅执行命令而且返回执行后的信息对象(常用于需要获取执行命令后的返回信息) ,读取结果是使用read方法,是阻塞模式,一旦读取到结果再次读取的时候返回内容为空. import os nowtime =