Python中调用shell
1 简单调用shell命令
os.system(command)
在一个子shell中运行command命令, 并返回command命令执行完毕后的退出状态. 这实际上是使用C标准库函数system()实现的. 这个函数在执行command命令时需要重新打开一个终端, 并且无法保存command命令的执行结果.
2 通过管道
os.popen(command, mode)
打开一个与command进程之间的管道. 这个函数的返回值是一个文件对象, 可以读或者写(由mode决定, mode默认是’r'). 如果mode为’r', 可以使用此函数的返回值调用read()来获取command命令的执行结果.
例如:
写管道:
os.popen('wc -c', 'w').write('hello world')
读管道:
print os.popen("echo 'hello world' |wc -c").read()
3 commands
import commands
commands.getstatusoutput(command)
使用os.popen()函数执行command命令并返回一个元组(status, output), 分别表示:
- command命令执行的返回状态(相当于命令行查看$?)
- 执行结果(相当于命令行输出)
对command的执行实际上是按照{command;} 2>&1的方式, 所以output中包含控制台输出信息或者错误信息. output中不包含尾部的换行符.
4 subprocess
- subprocess.call(['some_command','some_argument','another_argument_or_path'])
- subprocess.call(command,shell=True)
- subprocess.Popen(command, shell=True)
如果command不是一个可执行文件, shell=True不可省.
使用subprocess模块可以创建新的进程, 可以与新建进程的输入/输出/错误管道连通, 并可以获得新建进程执行的返回状态. 使用subprocess模块的目的是替代os.system(), os.popen(), commands.*等旧的函数或模块.
最简单的方法是使用class subprocess.Popen(command, shell=True). Popen类有Popen.stdin, Popen.stdout, Popen.stderr三个有用的属性, 可以实现与子进程的通信.
将调用shell的结果赋值给python变量. 如:
handle = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
print handle.communicate()[0]
>>> import subprocess
>>> path = subprocess.Popen("pwd", shell=True, stdout=subprocess.PIPE)
>>> print(path.communicate()[0])
/proc/4469
5 参考
http://blog.sina.cn/dpool/blog/s/blog_5357c0af0100yzet.html?vt=4
http://m.jb51.net/article/43500.htm
http://www.mikewootc.com/wiki/sw_develop/language/python_shell.html
Python中调用shell的更多相关文章
- [Python]在python中调用shell脚本,并传入参数-02python操作shell实例
首先创建2个shell脚本文件,测试用. test_shell_no_para.sh 运行时,不需要传递参数 test_shell_2_para.sh 运行时,需要传递2个参数 test_shell ...
- python 中调用shell命令
subprocess模块 根据Python官方文档说明,subprocess模块用于取代上面这些模块.有一个用Python实现的并行ssh工具—mssh,代码很简短,不过很有意思,它在线程中调用sub ...
- 【python中调用shell命令使用PIPE】使用PIPE作为stdout出现假卡死的情况——将stdout重定向为输出到临时文件
在Python中,调用:subprocess.Popen(cmd, stdout = PIPE, stderr = PIPE, shell= true)的时候,如果调用的shell命令本身在执行之后会 ...
- python中执行shell的两种方法总结
这篇文章主要介绍了python中执行shell的两种方法,有两种方法可以在Python中执行SHELL程序,方法一是使用Python的commands包,方法二则是使用subprocess包,这两个包 ...
- python中写shell(转)
python中写shell,亲测可用,转自stackoverflow To run a bash script, copy from stackoverflow def run_script(scri ...
- Awk中调用shell命令
Awk中调用shell命令 需求 在awk中,有时候需要调用linux系统中命令,如计算字符串的MD5值,并保存下来. 方法参考 call a shell command from inside aw ...
- 【转载】如何在C语言中调用shell命令
转载自:http://blog.csdn.net/chdhust/article/details/7951576 如何在C语言中调用shell命令 在linux操作系统中,很多shell命令使用起来非 ...
- [转] Makefile中调用Shell
1.在Makefile中只能在target中调用Shell脚本,其他地方是不能输出的.比如如下代码就是没有任何输出: VAR="Hello" echo "$(VAR)&q ...
- Python脚本传參和Python中调用mysqldump
Python脚本传參和Python中调用mysqldump<pre name="code" class="python">#coding=utf-8 ...
随机推荐
- 一个SAP顾问的回忆:我过去很胖!
去年也是这个时候,SAP成都研究院体育界大神邓阳,曾经赏脸在Jerry这个公众号上赐文一篇,介绍了他和围绕在他身边的一群小伙伴们的体育故事:SAP成都研究院的体育故事 而今天文章的主角则是SAP成都研 ...
- Spring Boot Start 打包方式装B指南
项目结构如下: test包:实际的代码 spring-boot-start-test包:start 配置包 代码详细配置如下 https://github.com/fqybzhangji/spring ...
- GOLANG利用断言调用结构体内特有的方法-
package main import( "fmt" _"sort" _"math/rand" ) //多态的特征是通过接口来实现的 //多 ...
- 数据库 Redis:Windows环境安装
1. 下载 Redis (1)前往 GitHub 下载:https://github.com/microsoftarchive/redis (2)点击 release : (3)选择好版本号后,下载文 ...
- 二叉堆的介绍和Java实现
一.堆和二叉堆 堆,英文名称Heap,所谓二叉堆(也有直接称二叉堆为堆的),本质上是一个完全二叉树,前面也提到过,如果树接近于完全二叉树或者满二叉树,采用顺序存储代价会小一点,因此常见的二叉堆均是顺序 ...
- 01—mybatis开山篇
什么是 MyBatis ? MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.M ...
- prometheus部署
1.prometheus安装 软件下载: wget https://dl.grafana.com/oss/release/grafana-6.4.2-1.x86_64.rpm https://gith ...
- Mybatis中的拦截器
作者:moshenglv的专栏 拦截器的一个作用就是我们可以拦截某些方法的调用,我们可以选择在这些被拦截的方法执行前后加上某些逻辑,也可以在执行这些被拦截的方法时执行自己的逻辑而不再执行被拦截的方法. ...
- 利用python deque的extend特性实现列表元素查重
from collections import deque mydquene = deque() mylist = [0,1,1,2,2,3,3,3,3,4,5,6,7,7,8,8,9,10,10,1 ...
- 2018-2019 ACM-ICPC Brazil Subregional Programming Contest B. Marbles(博弈)
题目链接:https://codeforc.es/gym/101908/problem/B 题意:两个人玩游戏,有 n 块石头,初始坐标为(x,y),一次操作可以将一块石头移动到(x - u,y),( ...