os.system() 和 os.popen()
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 相似的。
|
|
但是这样是无法获得到输出和返回值的,继续 Google,之后学会了 os.popen()。
|
|
通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出。但是怎么读取程序执行的返回值呢,当然咯继续请教伟大的 Google。Google 给我指向了 commands — Utilities for running commands。
这样通过 commands.getstatusoutput() 一个方法就可以获得到返回值和输出,非常好用。
|
|
Python Document 中给的一个例子,很清楚的给出了各方法的返回。
|
|
|
os.system 调用系统命令,完成后退出,返回结果是命令执行状态,一般是0 os.popen 可以实现一个“管道”,从这个命令获取的值可以在python 中继续被使用 os.popen使用语法如下: os.popen('CMD').readlines()[0] |
最近有个需求就是页面上执行shell命令,第一想到的就是os.system,
但是发现页面上打印的命令执行结果 0或者1,当然不满足需求了。
尝试第二种方案 os.popen()
print output.read()
通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出。但是无法读取程序执行的返回值)
尝试第三种方案 commands.getstatusoutput() 一个方法就可以获得到返回值和输出,非常好用。
print status, output
Python Document 中给的一个例子,
>>> 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()的更多相关文章
- 调用系统命令 os.system()和os.popen()
Python中os.system和os.popen区别 Python调用Shell,有两种方法:os.system(cmd)或os.popen(cmd)脚本执行过程中的输出内容.实际使用时视需求情况而 ...
- python os.system()和os.popen()
1>python调用Shell脚本,有两种方法:os.system()和os.popen(),前者返回值是脚本的退出状态码,后者的返回值是脚本执行过程中的输出内容.>>>hel ...
- python(45)内置函数:os.system() 和 os.popen()
os.system() 和 os.popen() 概述 os.popen() 方法用于从一个命令打开一个管道. 在Unix,Windows中有效 语法 popen()方法语法格式如下: os.pope ...
- Python os.system 和 os.popen的区别
(1) os.system # 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 system(command) -> exit_statusExecute the command ...
- os.system和os.popen
使用os.popen调用test.sh的情况: python调用Shell脚本,有两种方法:os.system(cmd)或os.popen(cmd),前者返回值是脚本的退出状态码,后者的返回值是脚本执 ...
- Python中os.system和os.popen区别
Python调用Shell,有两种方法:os.system(cmd)或os.popen(cmd)脚本执行过程中的输出内容.实际使用时视需求情况而选择. 两者的区别是: os.system(cmd)的返 ...
- python笔记16-执行cmd指令(os.system和os.popen)
os.system 1.如果想在cmd执行python脚本,可以直接用如下指令 python [xx.py绝对路径] 比如我写了个hello.py的脚本,在脚本里面写入内容:print("h ...
- Python执行系统命令的方法 os.system(),os.popen(),commands
os.popen():用python执行shell的命令,并且返回了结果,括号中是写shell命令 Python执行系统命令的方法: https://my.oschina.net/renwofei42 ...
- [py] os.system os.popen commands 执行shell
1.仅输出到屏幕,pwd保存的是状态<=====可用于执行shell命令 pwd=os.system(pwd) 2.popen可以保存命令结果 pwd=os.popen('pwd').r ...
随机推荐
- MFC里 显示设备上下文CClient dc(this) 和 CPaintDC dc(this)
1 CPaintDC类(1)CPaintDC类是CDC类的一个派生类,该类一般用在响应WM_PAINT消息的函数OnPaint()中.(2)WM_PAINT消息是当窗口的某个区域需要重画时激发的窗口消 ...
- UVa 12171 题解
英文题面不怎么友好,大家还是自行通过紫书了解题面吧... 解题思路: 1. 面对500 ^ 3的数据范围,我们需要先用离散化解决掉爆空间的问题. 2. 由于我们要求的总体积包括内空部分的体积,我们可以 ...
- vue使用原生js实现滚动页面跟踪导航高亮
需要使用vue做一个专题页面. 滚动页面指定区域导航高亮. BetterScroll:可能是目前最好用的移动端滚动插件 如何自定义CSS滚动条的样式? 监听滚动页面事件,对比当前页面的位置与元素的位置 ...
- Mac远程访问Ubuntu
MacOS和Ubuntu连接到同一个网络使用ping命令可以通信即可.SSH使用SSH可以很方便的在MacOS上访问Ubuntu,不过只能用命令行操作,相当于连接了Ubuntu的终端. 1. Ubun ...
- COMP9021--6.17
1. ''' '''the comment in the middle will be shown in your code while ranning 2. a=bc=a%bor we can si ...
- python爬取+使用网易卡搭作品数量api
第一步,当然是打开浏览器~ 然后打开卡搭~ 看着熟悉的界面,是不是有点不知所措? 这就对了,咱找点事情干干. 随便找个倒霉蛋,比如这位:"混世大王",打开他的主页! 按下f12(我 ...
- loj2256 「SNOI2017」英雄联盟
真的是裸背包啊-- #include <iostream> #include <cstdio> using namespace std; typedef long long l ...
- Zipkin和微服务链路跟踪
https://cloud.tencent.com/developer/article/1082821 Zipkin和微服务链路跟踪 本期分享的内容是有关zipkin和分布式跟踪的内容. 首先,我们还 ...
- C++ STL 的初步认知
学无止境!!! 尊重他人劳动,尊重出处:http://www.cnblogs.com/shiyangxt/archive/2008/09/11/1289493.html 我已经做了4年的MFC ...
- Jeddict:从服务器的验证过程,思考学习新事物的套路
结合一路研究Jeddict使用的过程经验来看,在这里说一下关于服务器配置的东西.在我们团队,最开始用这个插件的时候,因为公司用的应用服务器是Jboss EAP 7,所以,我们自然而然的,想当然的直接使 ...