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 — 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'

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 open() 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/python

import os, sys

# using command mkdir
a = '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()

os.system() 和 os.popen()的更多相关文章

  1. 调用系统命令 os.system()和os.popen()

    Python中os.system和os.popen区别 Python调用Shell,有两种方法:os.system(cmd)或os.popen(cmd)脚本执行过程中的输出内容.实际使用时视需求情况而 ...

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

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

  3. python(45)内置函数:os.system() 和 os.popen()

    os.system() 和 os.popen() 概述 os.popen() 方法用于从一个命令打开一个管道. 在Unix,Windows中有效 语法 popen()方法语法格式如下: os.pope ...

  4. Python os.system 和 os.popen的区别

    (1) os.system # 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 system(command) -> exit_statusExecute the command ...

  5. os.system和os.popen

    使用os.popen调用test.sh的情况: python调用Shell脚本,有两种方法:os.system(cmd)或os.popen(cmd),前者返回值是脚本的退出状态码,后者的返回值是脚本执 ...

  6. Python中os.system和os.popen区别

    Python调用Shell,有两种方法:os.system(cmd)或os.popen(cmd)脚本执行过程中的输出内容.实际使用时视需求情况而选择. 两者的区别是: os.system(cmd)的返 ...

  7. python笔记16-执行cmd指令(os.system和os.popen)

    os.system 1.如果想在cmd执行python脚本,可以直接用如下指令 python [xx.py绝对路径] 比如我写了个hello.py的脚本,在脚本里面写入内容:print("h ...

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

    os.popen():用python执行shell的命令,并且返回了结果,括号中是写shell命令 Python执行系统命令的方法: https://my.oschina.net/renwofei42 ...

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

      1.仅输出到屏幕,pwd保存的是状态<=====可用于执行shell命令 pwd=os.system(pwd)   2.popen可以保存命令结果 pwd=os.popen('pwd').r ...

随机推荐

  1. 基于Inception搭建MySQL SQL审核平台Yearing

    基于Inception搭建MySQL SQL审核平台Yearing Inception 1. Inceptionj简介 2. Inception安装 2.1 下载和编译 2.2 启动配置 Yearni ...

  2. centos7无法切换startx

    centos光盘安装后,显示命令行模式,通过startx无法进入图形界面? 解决方法:1.使用yum grouplist查看,根据显示的结果采用不同的界面格式,我用的是 yum groupinstal ...

  3. nginx基本安全优化

    一.调整参数隐藏nginx软件版本号信息 查看nginx版本信息: [root@nginx conf]# curl -I 192.168.200.102 HTTP/1.1 200 OK Server: ...

  4. Python开发环境与开发软件的安装

    Python开发的必要因素: 开发软件:PyCharm 社区版 PyCharm安装过程: 首先去官网下载:(链接为:  https://www.jetbrains.com/pycharm/downlo ...

  5. python-函数的对象、函数嵌套、名称空间和作用域

    目录 函数的对象 函数对象的四大功能 引用 当做参数传给一个函数 可以当做函数的返回值 可以当做容器类型的元素 函数的嵌套 函数的嵌套定义 函数的嵌套调用 名称空间与作用域 名称空间 内置名称空间 全 ...

  6. debian使用ibus

    $ sudo apt-get install ibus ibus-pinyin 点击右上角的键盘图标,设置拼音输入法

  7. BZOJ 4244: 邮戳拉力赛

    转化为括号序列DP 注意边界 #include<cstdio> #include<algorithm> #define rep(i,x,y) for (int i=x; i&l ...

  8. BZOJ 3326: [Scoi2013]数数

    数位DP,然而式子真的复杂 #include<cstdio> #include<algorithm> #include<cstring> using namespa ...

  9. 精通 JavaScript中的正则表达式

    精通 JS正则表达式 (精通?标题党 ) 正则表达式可以: •测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或一个信用卡号码模式.这称为数据有效性验证  ...

  10. ThreeJs 3D 全景项目开发总结

    本文来自网易云社区 作者:唐钊 项目背景 那是在一个毫无征兆的下午,我还沉浸在 vue 的世界中,突然编辑跑过来说N的新官网想做一些3D全景的东西,一开始其实我的内心是拒绝的,一是没怎么实质性做过 W ...