博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
os.system() 和 os.popen()
阅读量:4638 次
发布时间:2019-06-09

本文共 3419 字,大约阅读时间需要 11 分钟。

1.os.popen(command[, mode[, bufsize]])

  os.system(command)
2.os.popen() 功能强于os.system() , os.popen() 可以返回回显的内容,以文件描述符返回。
eg:
t_f = os.popen ("ping 192.168.1.1")
print t_f.read()
或者:
for line in os.popen("dir"):
    print line

 

最近在做那个测试框架的时候发现 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 给我指向了 。
这样通过 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'

 

os.system 调用系统命令,完成后退出,返回结果是命令执行状态,一般是0

os.popen 可以实现一个“管道”,从这个命令获取的值可以在python 中继续被使用

os.popen使用语法如下:

os.popen('CMD').readlines()[0]

 

 

最近有个需求就是页面上执行shell命令,第一想到的就是os.system,

复制代码代码如下:
os.system('cat /proc/cpuinfo')

但是发现页面上打印的命令执行结果 0或者1,当然不满足需求了。

 

尝试第二种方案 os.popen()

复制代码代码如下:
output = os.popen('cat /proc/cpuinfo')
print output.read()

通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出。但是无法读取程序执行的返回值)

 

尝试第三种方案 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.popen() Method

Description

The method popen() opens a pipe to or from command.The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'.The bufsize argument has the same meaning as in  function.

Syntax

Following is the syntax for popen() method:

os.popen(command[, mode[, bufsize]])

Parameters

  • command -- This is command used.

  • mode -- This is the Mode can be 'r'(default) or 'w'.

  • bufsize -- If the buffering value is set to 0, no buffering will take place. If the buffering value is 1, line buffering will be performed while accessing a file. If you specify the buffering value as an integer greater than 1, then buffering action will be performed with the indicated buffer size. If negative, the buffer size is the system default(default behavior). 

Return Value

This method returns an open file object connected to the pipe.

Example

The following example shows the usage of popen() method.

# !/usr/bin/pythonimport os, sys# using command mkdira = 'mkdir nwdir'b = os.popen(a,'r',1)print b

When we run above program, it produces following result:

open file 'mkdir nwdir', mode 'r' at 0x81614d0
调用c程序: fd=os.popen('/path/to/alignment arg1 arg2 arg3') output=fd.read() fd.close()

转载于:https://www.cnblogs.com/jefree/p/4461979.html

你可能感兴趣的文章
反射的所有api
查看>>
Js 判断网页窗口是否滚动到底部
查看>>
上传文件
查看>>
css 定位及遮罩层小技巧
查看>>
用java向mysql数据库中插入数据为空
查看>>
项目中非常有用并且常见的ES6语法
查看>>
dateTimePicker编辑状态下,取值不正确的问题
查看>>
mac 端口转发方案
查看>>
[2017.02.23] Java8 函数式编程
查看>>
loadrunner支持https协议的操作方法-经验总结
查看>>
Knowledge Point 20180305 数据在计算机中的表示
查看>>
谈谈对web标准的理解
查看>>
DIV+CSS规范命名大全集合
查看>>
求二进制中1的个数(编程之美2.1)
查看>>
hdu 4289 网络流拆点,类似最小割(可做模板)邻接矩阵实现
查看>>
58前端内推笔试2017(含答案)
查看>>
写给自己的web开发资源
查看>>
Java学习笔记
查看>>
sprintf 和strcpy 的差别
查看>>
打表打表何谓打表?
查看>>