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 ...
 
随机推荐
- elment-UI中表头和内容错位
			
当出现纵向滚动条的时候就错位了 网上找了很多方法发现对我的不生效 //把这样式添加到index.html中.或者app.vue中(必须是入口文件,才能全局起作用!)body .el-table th. ...
 - ES6-数组的扩展-整理
			
一.Array.from():负责把类似数组的对象以及可遍历的对象转为真正的数组 1.类似数组的对象 let arrayLike = { '0': 'a', '1': 'b', '2': 'c', l ...
 - 前端基础(三):JavaScript
			
JavaScript概述 JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中),后将其改名ScriptEase(客 ...
 - POI读取Excel数据
			
POI读取Excel表格数据 * {所需相关jar下载: * commons-collections4-4.4.jar * commons-compress-1.19.jar * poi-4.1.1. ...
 - 一个 TCP 连接可以发多少个 HTTP 请求?
			
曾经有这么一道经典面试题:从 URL 在浏览器被被输入到页面展现的过程中发生了什么?相信大多数准备过的同学都能回答出来,但是如果继续问:收到的 HTML 如果包含几十个图片标签,这些图片是以什么方式. ...
 - P1052 过河 题解
			
复习dp(迪皮)的时候刷到了一道简单路径压缩的题目(一点不会qwq) 题目描述链接. 正解: 首先呢,我们看到题目,自然而然的会想到这种思路: 设状态变量dp[i]表示从第一个格子开始经过一些跳跃跳到 ...
 - runloop   小记
			
一.什么是runLoop 1.说白了,runloop就是运行循环 2.runloop,他是多线程的法宝 通常来讲,一个线程一次只能执行一个任务,执行完之后就退出线程.但是,对于主线程是不能退出的,因此 ...
 - P5496 【模板】回文自动机(PAM)
			
做一下强制在线处理即可 #include <cstdio> #include <algorithm> #include <cstring> using namesp ...
 - Java队列与栈转换中String.Valueof()使用
			
1. 由 基本数据型态转换成 String String 类别中已经提供了将基本数据型态转换成 String 的 static 方法 也就是 String.valueOf() 这个参数多载的方法 有下 ...
 - BZOJ 2124  (线段树 + hash)
			
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2124 题意:给一个1到N的排列{Ai},询问是否存在1<=p1<p2< ...