Python 调用 Shell命令

python程序中调用shell命令,是件很酷且常用的事情
今天来总结一下

1.使用os模块 的  system
        此函数会启动子进程,在子进程中执行command,并返回command命令执行完毕后的 退出状态
如果command有执行内容,会在标准输出显示。这实际上是使用C标准库函数system()实现的。
       system方法会创建子进程运行外部程序,方法只返回外部程序的运行状态。这个方法比较适用于外部程序没有输出结果的情况
>>> import os
>>> os.system('ls')
1.py                   51.29.txt        dead.letter  error.txt      yue.csv
1.txt                  anaconda-ks.cfg  dengji.sh    kefuTongji.sh
0

>>> a=os.system('ls')
1.py                   51.29.txt        dead.letter  error.txt      yue.csv
1.txt                  anaconda-ks.cfg  dengji.sh    kefuTongji.sh
>>> print a
0


2.os模块的popen方法

        打开一个与command进程之间的管道。
这个函数的返回值是一个文件对象,可以读或者写(由mode决定,mode默认是’r')。如果mode为’r',可以使用此函数的返回值调用read()来获取command命令的执行结果。
        当需要得到外部程序的输出结果时,本方法非常有用,返回一个类文件对象,调用该对象的read()或readlines()方法可以读取输出内容
>>> os.popen('ls')
<open file 'ls', mode 'r' at 0x7f365a5075d0>

>>> print os.popen('ls').read()
1.py
1.txt

>>>a=os.popen('ls').readlines()
>>>print a
['1.py\n', '1.txt\n', '2016_11_28_access_log\n', '51.29.txt\n', 'anaconda-ks.cfg\n']


3.commands模块的  getoutput 方法

      这种方法同popend的区别在于popen返回的是一个类文件对象,而本方法将外部程序的输出结果当作字符串返回,很多情况下用起来要更方便些。

主要方法:  

*   commands.getstatusoutput(cmd)         返回(status, output)
*   commands.getoutput(cmd)                   只返回输出结果
*   commands.getstatus(file)                     返回ls -ld file的执行结果字符串,调用了getoutput,不建议


>>> import commands

>>> commands.getstatusoutput('ls')

(0, '1.py\n1.txt\n2016_11_28_access_log\n51.29.txt\nanaconda-ks.cfg\ndata.txt\ndead.letter\ndengji.sh\ndiskJK,sh')


>>> commands.getoutput('ls')

'1.py\n1.txt\n2016_11_28_access_log\n51.29.txt\nanaconda-ks.cfg'


>>> commands.getstatus('1.py')=ippi
'-rwxr-xr-x 1 root root 69 Jan 19 14:22 1.py'


4.模块subprocess

         subprocess意在替代其他几个老的模块或者函数,
比如:os.system os.spawn*  os.popen*  popen2.* commands.*

       根据Python官方文档说明,subprocess模块用于取代上面这些模块。有一个用Python实现的并行ssh工具—mssh,代码很简短,不过很有意思,它在线程中调用subprocess启动子进程来干活

subprocess.Popen('脚本/shell', shell=True)        无阻塞、和主程序并行运行
subprocess.call('脚本/shell', shell=True)              必须等待命令执行完毕

>>> import subprocess

>>> subprocess.Popen("ls")
1.py                   anaconda-ks.cfg  diskJK,sh      yue.csv
<subprocess.Popen object at 0x7fda1a0fcc50>

>>> a=subprocess.Popen("ls")
1.py                   anaconda-ks.cfg  diskJK,sh      yue.csv
>>> print a
<subprocess.Popen object at 0x7fda1a0fcc90>

>>>subprocess.Popen('. xxx.sh', stdout = subprocess.PIPE, shell = True).stdout.read()
'1.py\n1.txt\n2016_11_28_access_log\n51.29.txt\n'

  1. p = subprocess.Popen("python stdTest.py",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)  
  2. sout = p.stdout.readlines()  
  3. serr = p.stderr.readlines()  
  4. print sout  
  5. print serr  



>>> subprocess.call("ls")
1.py                   anaconda-ks.cfg  diskJK,sh      yue.csv

>>> a=subprocess.call("ls")
1.py                   anaconda-ks.cfg  diskJK,sh      yue.csv
>>> print a
0


>>> from subprocess import call
>>> call(["ls","-l"])
total 2972
-rwxr-xr-x 1 root root     220 Jan 17 22:33 1.sh



总结一下:
1.os.system            命令行可以输出运行内容,但只能返回 运行状态
2.os.popen             返回一个类文件对象,可以从此文件读取 输出内容
3.commands.get    既能返回 输出内容,也能返回 运行结果状态
4.subprocess           既能返回 输出内容 ,也能返回 运行结果状态
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值