python 调用Linux shell
有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的。那么我们使用Python如何调用Linux的Shell命令?下面来介绍几种常用的方法:
1. os 模块
1.1. os模块的exec方法族
Python的exec系统方法同Unix的exec系统调用是一致的。这些方法适用
于在子进程中调用外部程序的情况,因为外部程序会替换当前进程的代码,不会返回。( 这个看了点 help(os) --> search
"exec" 的相关介绍,但是没太搞明白咋使用)
1.2. os模块的system方法
system方法会创建子进程运行外部程序,方法只返回外部程序的运行结果。这个方法比较适用于外部程序没有输出结果的情况。
- >>> import os
- >>> os.system("echo \"Hello World\"") # 直接使用os.system调用一个echo命令
- Hello World ——————> 打印命令结果
- 0 ——————> What's this ? 返回值?
- >>> val = os.system("ls -al | grep \"log\" ") # 使用val接收返回值
- -rw-r--r-- 1 root root 6030829 Dec 31 15:14 log ——————> 此时只打印了命令结果
- >>> print val
- 0 ——————> 注意,此时命令正常运行时,返回值是0
- >>> val = os.system("ls -al | grep \"log1\" ")
- >>> print val
- 256 ——————> 使用os.system调用一个没有返回结果的命令,返回值为256~
- >>>
注意:上面说了,此方法脂肪会外部程序的结果,也就是os.system的结果,所以如果你想接收命令的返回值,接着向下看~
1.3. os模块的popen方法
当需要得到外部程序的输出结果时,本方法非常有用,返回一个类文件对象,调用该对象的read()或readlines()方法可以读取输出内容。比如使用urllib调用Web API时,需要对得到的数据进行处理。os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 如a=os.popen(cmd).read()
- >>> os.popen('ls -lt') # 调用os.popen(cmd)并不能得到我们想要的结果
- <open file 'ls -lt ', mode 'r' at 0xb7585ee8>
- >>> print os.popen('ls -lt').read() # 调用read()方法可以得到命令的结果
- total 6064
- -rwxr-xr-x 1 long long 23 Jan 5 21:00 hello.sh
- -rw-r--r-- 1 long long 147 Jan 5 20:26 Makefile
- drwxr-xr-x 3 long long 4096 Jan 2 19:37 test
- -rw-r--r-- 1 root root 6030829 Dec 31 15:14 log
- drwxr-xr-x 2 long long 4096 Dec 28 09:36 pip_build_long
- drwx------ 2 Debian-gdm Debian-gdm 4096 Dec 23 19:08 pulse-gylJ5EL24GU9
- drwx------ 2 long long 4096 Jan 1 1970 orbit-long
- >>> val = os.popen('ls -lt').read() # 使用变量可以接收命令返回值
- >>> if "log" in val: # 我们可以使用in来判断返回值中有木有一个字符串
- ... print "Haha,there is the log"
- ... else:
- ... print "No,not happy"
- ...
- Haha,there is the log
2. commands 模块
使用commands模块的getoutput方法,这种方法同popend的区别在于popen返回的是一个类文件对象,而本方法将外部程序的输出结果当作字符串返回,很多情况下用起来要更方便些。
主要方法:
* commands.getstatusoutput(cmd) 返回(status, output)
* commands.getoutput(cmd) 只返回输出结果
* commands.getstatus(file) 返回ls -ld file的执行结果字符串,调用了getoutput,不建议使用此方法
- long@zhouyl:/tmp/tests$ python
- Python 2.7.3 (default, Jan 2 2013, 16:53:07)
- [GCC 4.7.2] on linux2
- Type "help", "copyright", "credits" or "license" for more information.
- >>> import commands
- >>> commands.getstatusoutput('ls -lt') # 返回(status, output)
- (0, 'total 5900\n-rwxr-xr-x 1 long long 23 Jan 5 21:34 hello.sh\n-rw-r--r-- 1 long long 147 Jan 5 21:34 Makefile\n-rw-r--r-- 1 long long 6030829 Jan 5 21:34 log')
- >>> commands.getoutput('ls -lt') # 返回命令的输出结果(貌似和Shell命令的输出格式不同哈~)
- 'total 5900\n-rwxr-xr-x 1 long long 23 Jan 5 21:34 hello.sh\n-rw-r--r-- 1 long long 147 Jan 5 21:34 Makefile\n-rw-r--r-- 1 long long 6030829 Jan 5 21:34 log'
- >>> commands.getstatus('log') # 调用commands.getoutput中的命令对'log'文件进行相同的操作
- '-rw-r--r-- 1 long long 6030829 Jan 5 21:34 log'
- >>>
3. subprocess模块
根据Python官方文档说明,subprocess模块用于取代上面这些模块。有一个用Python实现的并行ssh工具—mssh,代码很简短,不过很有意思,它在线程中调用subprocess启动子进程来干活。
- >>> from subprocess import call
- >>> call(["ls", "-l"])
subprocess与system相比的优势是它更灵活(你可以得到标准输出,标准错误,“真正”的状态代码,更好的错误处理,等..)。我认为使用os.system已过时,或即将过时。
4. 众方法的比较以及总结
4.1. 关于 os.system
os.system("some_command with args")将命令以及参数传递给你的系统shell,这很好,因为你可以用这种方法同时运行多个命令并且可以设置管道以及输入输出重定向。比如:
os.system("some_command < input_file | another_command > output_file")
然而,虽然这很方便,但是你需要手动处理shell字符的转义,比如空格等。此外,这也只能让你运行简单的shell命令而且不能运行外部程序。
4.2. 关于os.popen
使用stream = os.popen("some_command with args")也能做与os.system一样的事,与os.system不同的是os.popen会返回一个类文件对象,使用它来访问标准输入、输出。
4.3. 关于subprocess.popen
subprocess模块的Popen类,意图作为os.popen的替代,但是因为其很全面所以比os.popen要显得稍微复杂。
比如你可以使用 print Popen("echo Hello World", stdout=PIPE,
shell=True).stdout.read() 来替代 print os.popen("echo Hello
World").read()。但是相比之下它使用一个统一的类包括4中不同的popen函数还是不错的。
4.4. 关于subprocess.call
subprocess模块的call函数。它基本上就像Popen类并都使用相同的参数,但是它只简单的等待命令完成并给你返回代码。比如:
return_code = subprocess.call("echo Hello World", shell=True)
os模块中还有C中那样的fork/exec/spawn函数,但是我不建议直接使用它们。subprocess可能更加适合你。
===========================================
[1] http://demi-panda.com/2013/01/25/python-shell-command/index.html
[2] http://m.blog.csdn.net/blog/overstack/9295995
[3] http://blog.csdn.net/swiftshow/article/details/7755543
下面是对于文中所涉及的内容的python官方文档:
[4] http://docs.python.org/library/subprocess.html#replacing-older-functions-with-the-subprocess-module
-- 关于使用subprocess 替代老的方法
[5] http://docs.python.org/lib/os-process.html -- os的exec方法族以及system方法
[6] http://docs.python.org/lib/os-newstreams.html -- os的popen方法
[7] http://docs.python.org/lib/node528.html -- os的subprocess介绍
python 调用Linux shell的更多相关文章
- 举例讲解Linux系统下Python调用系统Shell的方法
有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的.那么我们使用Python如何调用Linux的Shell命令?下面来介绍几种常用的方法:1. os 模块 ...
- python 之调用Linux shell命令及相关高级应用
最近根据老大要求,将数据进行同步备份,结合第三方提供的工具.第三方服务其实是有python demo的,本想研究下实际的python sdk搞个demo开发的,但是发现有些组建装起来确实头大,而且本公 ...
- python调用linux的命令
有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的.那么我们使用Python如何调用Linux的Shell命令?下面来介绍几种常用的方法: 1. os 模块 ...
- python调用系统命令 shell命令
使用python调用系统命令,基本有3种选择: 1. 使用os模块的system方法 import os os.system('ls') 2. 使用os模块的popen方法 import os os. ...
- python 调用 bash (python 调用linux命令)
原文这里有显示地址:http://zhou123.blog.51cto.com/4355617/1312791 现在摘取一部分: 这里介绍一下python执行shell命令的四种方法: 1.os模块中 ...
- Python调用Linux bash命令
import subprocess as sup # 以下注释很多(为了自己以后不忘), 如果只是想在python中执行Linux命令, 看前5行就够了 # 3.5版本之后官方推荐使用sup.run ...
- 关于java调用linux shell 的问题
问题的提出: shell脚本要做离线的数据处理任务 java调用脚本,将这种处理任务封装成webservice 特点: shell处理单个时间长 每次要处理文件量大 这里目前只做调用分析: 原来的: ...
- python调用Linux下so文件
1.通过C语言编写一个简单max函数,生成一个max.so链接库 /* * # -shared 为链接库 让编译器知道是要编译一个共享库 * # -fPIC(Position Independent ...
- python执行linux shell管道输出内容
干净不留痕,用过都说好. echo "print 1+1" |python
随机推荐
- Eclipse AmaterasUML 安装及使用
AmaterasUML 对于我来说,是一个非常好用的UML插件. 用它来将我写过的一些Android程序进行逆工程非常好用,只不过,不能体现出包,这是一个小小的遗憾. 这个是它的主页地址:http:/ ...
- CS229 1
1.机器学习 机器学习是工具,具体应用到某个实际场景下,才是目的. 2.分类 a 监督学习,包括回归(regression),分类(classification).回归问题,数据可以是连续或者离散,分 ...
- Qt Qwdget 汽车仪表知识点拆解8 淡入效果
先贴上效果图,注意,没有写逻辑,都是乱动的 看下面的开始,开始的时候有一个带入的效果,这里有一个坑, 网上大部分都是调用下面这个函数 setWindowOpacity(); 但是,你会发现,在你的子窗 ...
- 单链表无head各种操作及操作实验
#encoding=utf-8 class ListNode: def __init__(self,x): self.val=x; self.next=None; #链表逆序 def revers ...
- tensorflow nmt基本配置(tf-1.4)
随着tensorflow的不断更新,直接按照nmt的教程搭建nmt环境会报错的...因此,需要一些不太好的办法来避免更多的问题出现.tensorflow看来在ubuntu和debian中运行是没有问题 ...
- 剑指offer-数值的整数次方12
class Solution: def Power(self, base, exponent): # write code here if base==0: return 0 if exponent= ...
- %matplotlib inline
整理摘自 https://zhidao.baidu.com/question/1387744870700677180.html %matplotlib inline是jupyter notebook里 ...
- Sublime Text 3配置 Python3 开发环境
来自 https://www.cnblogs.com/zhangqinwei/p/6886600.html Sublime Text作为一款支持多种编程语言的文本编辑神器,深受广大开发者的喜爱.通过简 ...
- HDU 4467 Graph(图论+暴力)(2012 Asia Chengdu Regional Contest)
Description P. T. Tigris is a student currently studying graph theory. One day, when he was studying ...
- python学习笔记-list的用法
1.list的定义 list = [] list = [1,2,'a','b'](list中的元素不一定是一个类型) 2.list的操作 1)list.append(value) 2)list.ins ...