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 ...
随机推荐
- zend studio10 破解方法
zend studio 是一款强大的PHP开发工具,该软件的具体信息用户可以百度一下,下面是zend studio10的破解方法. 1.下载zend studio 10,下载地址:链接:http:// ...
- 欧拉函数:HDU3501-Calculation 2
Calculation 2 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- 打印机增强软件pdfpro
http://3dx.pc6.com/gm1/pdfpro.zip
- LA 3790 Overlapping Squares DFS
题意: 给出一个字符矩阵,问能否是不超过6个2×2的正方形组成的. 分析: 每次找一个最表面的正方形然后DFS就好了. 一个正方形被移开后,用一个特殊符号标记上,下次再匹配的时候就直接忽略这些位置. ...
- 一个JS判断客户端是否已安装某个字体(Only IE)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 自建NAS如何使用大于2TB的硬盘(从分区开始)
目录 自建NAS如何使用大于2TB的硬盘(从分区开始) 对分区进行格式化 挂载到某一目录(需设置开机自动挂载) 上传文件测试: 补充 自建NAS如何使用大于2TB的硬盘(从分区开始) 需求说明: 自建 ...
- 让 PHP COOKIE 立即生效(不用刷新就可以使用)
<?php function set_my_cookie($, $path = '', $domain = '') { $_COOKIE[$var] = $value; setcookie($v ...
- [Android Studio篇][1] AS开发中遇到问题汇总
1 在android新建文件,提示权限不够,增加权限 修改工程下 main/AndroidMainfest.xml增加 <uses-permission android:name="a ...
- maven项目打包jar,含有依赖jar
在pom文件中添加一下插件 <plugin> <artifactId>maven-assembly-plugin</artifactId> <configur ...
- d3 画简单的柱形图
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...