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 ...
随机推荐
- 利用浏览器做好数字进制和ASCII码及Unicode教与学
浏览器是现在个人计算机的标配,一般来说一个PC至少安装一种以上的浏览器.主流网页浏览器有Google Chrome.Internet Explorer.Microsoft Edge.Mozilla F ...
- 浅谈Promise原理与应用
在JavaScript中,所有代码都是单线程.由于该“缺陷”,JavaScript在处理网络操作.事件操作时都是需要进行异步执行的.AJAX就是一个典型的异步操作 对于异步操作,有传统的利用回调函数和 ...
- 【python】写csv文件时遇到的错误
1.错误 在许多文件中,写入csv文件时都加"wb",w指写入,b指二进制 如: csvwrite=csv.writer(open("output.csv",& ...
- 《Linux就该这么学》day4-6
继续学习打卡,这几天有事所以看视频补上了这几天的学习进度. day4:主要重点 tar打包和解压命令 tar -zxvf xxx.tar.gz 解压xxx.tar.gz (将xxx.tar.gz ...
- win10家庭版设置移动热点出现“我们无法设置移动热点”
寝室wifi卡到爆炸, 买了一个360随身WiFi,可是360随身WiFi烧坏了 ...然后我就一个星期没玩游戏了 今天本来想开电脑的wifi试一试,结果发现无法设置热点 纳闷了 百度一下,发现都 ...
- IIS教程:因权限问题被拒绝访问的解决方案
https://blog.csdn.net/a497785609/article/details/49952281 写了一个类IISAdmin,负责建立.设置.删除虚拟目录,发现在web中调用,遇到权 ...
- linux(1)
Linux/Unix操作系统 OS 系统软件 用户.应用程序 <-OS-> 硬件:CPU Memory Disk 外设管理软件测试方向: 被测系统主要的操作系统,监控系统资源.使用系统常用 ...
- redis四种部署方式
1.单点 2.主从 3.哨兵 4.集群
- C# ado.net 操作存储过程(二)
调用存储过程 sql IF OBJECT_ID('RegionInsert') IS NULL EXEC (' -- -- Procedure which inserts a region recor ...
- httpclient发邮件
package com.chuanglan; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.L ...