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/pythonimport 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()

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

os.system() 和 os.popen()相关推荐

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

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

  2. windows popen 获取不到输出_彻底明白os.system、os.popen、subprocess.popen的用法和区别...

    Hello,大家好,沉寂许久,我又来了,这次给大家分享的就是上述三个方法的使用范围和区别,不熟悉的可以在这个地方画一个圆满的句号了. os.system 首先来看这个函数的文档说明 是说是在一个子sh ...

  3. python执行系统命令的方法_python执行系统命令的方法 :os.system(),subprocess.popen(),command...

    最近接触到os.system(),subprocess.popen()和commands来执行系统命令,从网上搜索到许多,现整理如下. 1. 使用os.system("cmd") ...

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

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

  5. Python面试高频问题: os.system()和os.popen()的区别

    os.system()和os.popen()概述 大家搞python与操作系统交互时,必须掌握的两个方法就是os.system()和os.popen(),也是在相关技术领域面试中必问的题目!本文就对这 ...

  6. python中os system_Python调用系统命令os.system()和os.popen()的实现

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

  7. os.system和os.popen和commands

    os.system和os.popen os.system(cmd)的返回值是脚本的退出状态码,只会有0(成功),1,2 os.popen(cmd)返回脚本执行的输出内容作为返回值 当你想把执行命令返回 ...

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

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

  9. os.system() 和os.popen()的区别

    os.system() 和os.popen()的区别 返回的数据不同 1 os.system("ls")  返回0 但是这样是无法获得到输出和返回值的 继续 Google,之后学会 ...

  10. python中os.system、os.popen、subprocess.popen的区别

    最近项目中需要在python中执行shell脚本,以下解释使用os.system. os.popen和subprocess.popen的区别: 1.os.system 该函数返回命令执行结果的返回值, ...

最新文章

  1. Android动画曲线库AndroidEasingFunctions
  2. 【计算理论】计算理论总结 ( 正则表达式转为非确定性有限自动机 NFA | 示例 ) ★★
  3. java gettext用法_Java Context.getText方法代码示例
  4. java中可以用浮点作为循环变量吗_Java千问:Java循环语句的几个冷门知识点你都知道吗?...
  5. vmware服务器虚拟化实施文档,VMware服务器虚拟化
  6. bat复制文件到指定目录同名_scp复制文件时排除指定文件
  7. 升级后重启造成fsck.ext3: Unable to resolve UUID
  8. Javascript 运行上下文和作用域链
  9. 2017 让机器给我们干活
  10. Cmake构建_指定gcc/g++版本
  11. 您的计算机无法加入域,Win7系统电脑无法加入域提示找不到网络路径的应对方案...
  12. 手机html像素,手机分辨率和网页中的PX是一回事吗?
  13. 《如何高效学习》读后感
  14. 自定义角色外观之捏脸
  15. ArcGIS10.8安装包以及教程
  16. mplfinance 一个堪称完美python量化金融可视化工具详析
  17. 大学生必备:用Python实现shua课自由,又是美好的一天
  18. 南邮部分期末复习笔记汇总@tou
  19. 赫兹,雷诺,使电动汽车的欧洲租赁市场
  20. 微信公众平台开发订阅号

热门文章

  1. 2022-2028年中国女式西装行业研究及前瞻分析报告
  2. Redis 笔记(06)— set 类型(向集合添加元素、获取集合元素个数、判断集合中是否包含某个元素、删除给定元素、返回集合中所有元素、计算集合的交集、并集、差集)
  3. 数据库 user schema sqlserver 关系
  4. LeetCode简单题之较大分组的位置
  5. 光学传输与摄像头光学技术
  6. cuGraph-GPU图形分析
  7. Timer定时器开发
  8. docker 实现redis集群搭建
  9. Cocos 发射和监听事件 事件派送(TypeScript)
  10. Python 元组的使用