一、commands模块

1、介绍

当我们使用Python进行编码的时候,但是又想运行一些shell命令,去创建文件夹、移动文件等等操作时,我们可以使用一些Python库去执行shell命令。

commands模块就是其中的一个可执行shell命令的库,commands模块是python的内置模块,共有三个函数:

  1. getstatus(file):返回执行 ls -ld file 命令的结果( -ld 代表的是仅列出指定目录的详细信息)。
  2. getoutput(cmd):执行cmd命令,并返回输出的内容,返回结果为str。
  3. getstatusoutput(cmd):执行cmd命令,并返回执行的状态(status)和输出的内容(output),status代表的shell命令的返回状态,如果成功的话是0,output是shell的返回的结果。

注意:

commands从2.6版开始不推荐使用:该模块已在Python 3中删除。推荐使用subprocess模块(等下再介绍)。

在3.x版本中,getstatus()方法被移除,getoutput()和getstatusoutput()被放到了subprocess模块中。

2、getstatus(file)

返回执行 ls -ld file 命令的结果( -ld 代表的是仅列出指定目录的详细信息)。

# -*- coding: utf-8 -*-
import commands
status = commands.getstatus("/opt") # 即执行了:ls -ld /opt
print status # 结果
drwxr-xr-x. 9 root root 4096 2019/11/11 16:49:40 /opt

3、getoutput(cmd)

执行cmd命令,并返回输出的内容,返回结果为str。

# -*- coding: utf-8 -*-
import commands
output = commands.getoutput("ls -l /opt")
print output # 结果
总用量 28
drwx--x--x 4 root root 4096 2019/11/11 16:49:40 containerd
drwxr-xr-x 13 root root 4096 2019/01/15 14:48:15 nginx1.12
drwxrwxr-x 7 500 500 4096 2019/01/14 10:30:05 node-v8.6.0-linux-x64
drwxr-xr-x 7 root root 4096 2019/09/17 09:31:24 Projects
drwxr-xr-x 6 root root 4096 2019/01/10 19:17:30 python36
drwxrwxr-x 8 root root 4096 2019/09/16 20:00:52 redis-4.0.10
drwxr-xr-x 6 root root 4096 2019/01/16 17:47:34 ruby

4、getstatusoutput(cmd)

执行cmd命令,并返回执行的状态(status)和输出的内容(output),status代表的shell命令的返回状态,如果成功的话是0,output是shell的返回的结果。

# -*- coding: utf-8 -*-
import commands
status, output = commands.getstatusoutput("ls -l /opt")
print "status: %s" % status
print "output: %s" % output # 结果
status: 0
output: 总用量 28
drwx--x--x 4 root root 4096 2019/11/11 16:49:40 containerd
drwxr-xr-x 13 root root 4096 2019/01/15 14:48:15 nginx1.12
drwxrwxr-x 7 500 500 4096 2019/01/14 10:30:05 node-v8.6.0-linux-x64
drwxr-xr-x 7 root root 4096 2019/09/17 09:31:24 Projects
drwxr-xr-x 6 root root 4096 2019/01/10 19:17:30 python36
drwxrwxr-x 8 root root 4096 2019/09/16 20:00:52 redis-4.0.10
drwxr-xr-x 6 root root 4096 2019/01/16 17:47:34 ruby

二、subprocess模块

1、介绍

subprocess模块允许你启动一个新的进程,连接输入/输出/错误的管道, 获得子进程的返回码。这个模块目标是代替一些老的模块,比如os.system和os.spawn。

subprocess模块中的常用函数

函数 描述
subprocess.getoutput(cmd) 接收字符串格式的命令,执行命令并返回执行结果,其功能类似于os.popen(cmd).read()和commands.getoutput(cmd)。
subprocess.getstatusoutput(cmd) 执行cmd命令,返回一个元组(命令执行状态, 命令执行结果输出),其功能类似于commands.getstatusoutput()。
subprocess.call() 执行指定的命令,返回命令执行状态,其功能类似于os.system(cmd)。
subprocess.check_call() Python 2.5中新增的函数。 执行指定的命令,如果执行成功则返回状态码,否则抛出异常。其功能等价于subprocess.run(..., check=True)。
subprocess.check_output() Python 2.7中新增的的函数。执行指定的命令,如果执行状态码为0则返回命令执行结果,否则抛出异常。
subprocess.run() Python 3.5中新增的函数。执行指定的命令,等待命令执行完成后返回一个包含执行结果的CompletedProcess类的实例。

2、getoutput,getstatusoutput

上面我们说了,commands在3.x版本中,getstatus()方法被移除,getoutput()和getstatusoutput()被放到了subprocess模块中。

因此subprocess中的getoutput,getstatusoutput用法与commands的用法一模一样。

# -*- coding: utf-8 -*-
import subprocess
output = subprocess.getoutput("pwd")
print("output1: %s" % output) status, output = subprocess.getstatusoutput("pwd")
print("status2: %s" % status)
print("output2: %s" % output) # 结果
output1: /tmp status2: 0
output2: /tmp

3、subprocess.call()

返回值为命令执行状态码;
若未指定stdout,则命令执行后的结果输出到屏幕;
若指定stdout,则命令执行后的结果输出到stdout;
若执行成功,则函数返回值为0;若执行失败,则函数返回值为1;

执行命令,输出到屏幕(终端),返回状态码(命令正常执行返回0,其他状态码都是错误状态码)

subprocess.call(cmd,[shell=False,stdout]) 当shell=False的时候(默认),cmd为一个列表,当shell=True的时候,cmd为一个字符串,例如:

# -*- coding:utf-8 -*-
import subprocess try:
# shell=False
ret1 = subprocess.call(["ls", "-l", "/opt"], shell=False)
print("result1: %s" % ret1) # shell=True
ret2 = subprocess.call("lxxs -l /tmp", shell=True) # 当命令是错误的时候,返回的状态码就不是0了
print("result2: %s" % ret2)
except Exception as e:
print(e) # 结果
总用量 28
drwx--x--x 4 root root 4096 2019/11/11 16:49:40 containerd
drwxr-xr-x 13 root root 4096 2019/01/15 14:48:15 nginx1.12
drwxrwxr-x 7 500 500 4096 2019/01/14 10:30:05 node-v8.6.0-linux-x64
drwxr-xr-x 7 root root 4096 2019/09/17 09:31:24 Projects
drwxr-xr-x 6 root root 4096 2019/01/10 19:17:30 python36
drwxrwxr-x 8 root root 4096 2019/09/16 20:00:52 redis-4.0.10
drwxr-xr-x 6 root root 4096 2019/01/16 17:47:34 ruby
result1: 0 /bin/sh: lxxs: 未找到命令
result2: 127

4、subprocess.check_call()

返回值为命令执行状态码;
若未指定stdout,则命令执行后的结果输出到屏幕;
若指定stdout,则命令执行后的结果输出到stdout;
若执行成功,则函数返回值为0;若执行失败,抛出异常;

执行命令,输出到屏幕(终端),如果执行成功则返回状态码0,否则抛异常(subprocess.CalledProcessError)。

其实check_call基本和call功能一样,只是增加了返回状态码校验,如果执行状态码是0,则返回0,否则抛出异常

# -*- coding:utf-8 -*-
import subprocess try:
ret = subprocess.check_call("ls -l /opt", shell=True)
print("result: %s" % ret)
except subprocess.CalledProcessError as e:
print(e)

5、subprocess.check_output()

返回值为命令执行的输出结果;
若执行成功,则函数返回值为命令输出结果;若执行失败,则抛出异常;

执行命令,不会输出到屏幕(终端),如果执行成功则返回执行结果,否则抛异常

# -*- coding:utf-8 -*-
import subprocess try:
ret = subprocess.check_output("ls -l /opt", shell=True)
print("result: %s" % ret)
except subprocess.CalledProcessError as e:
print(e) # 结果
result: b'\xe6\x80\xbb\xe7\x94\xa8\xe9\x87\x8f 28\ndrwx--x--x 4 root root 4096 2019/11/11 16:49:40 containerd\ndrwxr-xr-x 13 root root 4096 2019/01/15 14:48:15 nginx1.12\ndrwxrwxr-x 7 500 500 4096 2019/01/14 10:30:05 node-v8.6.0-linux-x64\ndrwxr-xr-x 7 root root 4096 2019/09/17 09:31:24 Projects\ndrwxr-xr-x 6 root root 4096 2019/01/10 19:17:30 python36\ndrwxrwxr-x 8 root root 4096 2019/09/16 20:00:52 redis-4.0.10\ndrwxr-xr-x 6 root root 4096 2019/01/16 17:47:34 ruby\n'

6、subprocess.Popen()

class subprocess.Popen( args,
bufsize=0,
executable=None,
stdin=None,
stdout=None,
stderr=None,
preexec_fn=None,
close_fds=False,
shell=False,
cwd=None,
env=None,
universal_newlines=False,
startupinfo=None,
creationflags=0)

各参数含义如下:

args:

args参数。可以是一个字符串,可以是一个包含程序参数的列表。要执行的程序一般就是这个列表的第一项,或者是字符串本身。

subprocess.Popen(["cat","test.txt"],shell=False)
subprocess.Popen("cat test.txt", shell=True)

这个命令实际上

subprocess.Popen("cat test.txt", shell=True)

相当于

subprocess.Popen(["/bin/sh", "-c", "cat test.txt"])

在*nix下,当shell=False(默认)时,Popen使用os.execvp()来执行子程序。args一般要是一个[列表]。如果args是个字符串的话,要设置shell=True

Popen的一些方法

subprocess.Popen()


class Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
实际上,上面的几个函数都是基于Popen()的封装(wrapper)。这些封装的目的在于让我们容易使用子进程。当我们想要更个性化我们的需求的时候,就要转向Popen类,该类生成的对象用来代表子进程。

与上面的封装不同,Popen对象创建后,主程序不会自动等待子进程完成。我们必须调用对象的wait()方法,父进程才会等待 (也就是阻塞block),举例:

>>> import subprocess
>>> child = subprocess.Popen(['ping','-c','4','blog.linuxeye.com'])
>>> print 'parent process'

从运行结果中看到,父进程在开启子进程之后并等待child的完成后,再运行print。
此外,你还可以在父进程中对子进程进行其它操作,比如我们上面例子中的child对象:

child.poll() # 检查子进程状态
child.kill() # 终止子进程
child.send_signal() # 向子进程发送信号
child.terminate() # 终止子进程
子进程的PID存储在child.pid

子进程的文本流控制

子进程的标准输入、标准输出和标准错误如下属性分别表示:

child.stdin
child.stdout
child.stderr
可以在Popen()建立子进程的时候改变标准输入、标准输出和标准错误,并可以利用subprocess.PIPE将多个子进程的输入和输出连接在一起,构成管道(pipe),如下2个例子:

>>> import subprocess
>>> child1 = subprocess.Popen(["ls","-l"], stdout=subprocess.PIPE)
>>> print child1.stdout.read(),
#或者child1.communicate()
>>> import subprocess
>>> child1 = subprocess.Popen(["cat","/etc/passwd"], stdout=subprocess.PIPE)
>>> child2 = subprocess.Popen(["grep","0:0"],stdin=child1.stdout, stdout=subprocess.PIPE)
>>> out = child2.communicate()

subprocess.PIPE实际上为文本流提供一个缓存区。child1的stdout将文本输出到缓存区,随后child2的stdin从该PIPE中将文本读取走。child2的输出文本也被存放在PIPE中,直到communicate()方法从PIPE中读取出PIPE中的文本。
注意:communicate()是Popen对象的一个方法,该方法会阻塞父进程,直到子进程完成

7、call、check_call、check_output的区别

根据上面的结果,我们可知:

  • ret1 = subprocess.call(cmd):ret1是cmd命令执行后的状态码,cmd执行的结果会在终端显示出来,也就是说如果不需要判断命令的执行结果的状态码,直接subprocess.call(cmd)即可,不需要用一个变量去接收状态码。
  • ret2 = subprocess.check_call(cmd):跟call一样的,只是如果状态码不是0,即命令执行失败的时候会抛出异常。
  • ret3 = subprocess.check_output(cmd): call和check_call的ret是cmd命令的状态码,cmd的执行结果是在终端显示的,而check_output的cmd执行结果不会显示在终端,而是保存在ret中。

python之commands和subprocess入门介绍(可执行shell命令的模块)的更多相关文章

  1. python的subprocess模块执行shell命令

    subprocess模块可以允许我们执行shell命令 一般来说,使用run()方法就可以满足大部分情况 使用run执行shell命令 In [5]: subprocess.run('echo &qu ...

  2. python中执行shell命令行read结果

    +++++++++++++++++++++++++++++ python执行shell命令1 os.system 可以返回运行shell命令状态,同时会在终端输出运行结果 例如 ipython中运行如 ...

  3. python中执行shell命令的几个方法小结(转载)

    转载:http://www.jb51.net/article/55327.htm python中执行shell命令的几个方法小结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014- ...

  4. subprocess模块还提供了很多方便的方法来使得执行 shell 命令

    现在你可以看到它正常地处理了转义. 注意 实际上你也可以在shell=False那里直接使用一个单独的字符串作为参数, 但是它必须是命令程序本身,这种做法和在一个列表中定义一个args没什么区别.而如 ...

  5. python(6)-执行shell命令

    可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen*          --废弃 popen2.*           --废弃 commands.* ...

  6. python 执行shell命令

    1.os模块中的os.system()这个函数来执行shell命令 1 2 3 >>> os.system('ls') anaconda-ks.cfg  install.log  i ...

  7. python中执行shell命令的几个方法小结

    原文 http://www.jb51.net/article/55327.htm 最近有个需求就是页面上执行shell命令,第一想到的就是os.system, os.system('cat /proc ...

  8. C++/Php/Python 语言执行shell命令

    编程中经常需要在程序中使用shell命令来简化程序,这里记录一下. 1. C++ 执行shell命令 #include <iostream> #include <string> ...

  9. 利用commands模块执行shell命令

    利用commands模块执行shell命令 用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态 ...

随机推荐

  1. springboot demo(二)web开发demo

    如入门般建立项目,引入依赖: <dependencies> <dependency> <groupId>org.springframework.boot</g ...

  2. mybatis(六)插件机制及分页插件原理

    转载:https://www.cnblogs.com/wuzhenzhao/p/11120848.html MyBatis 通过提供插件机制,让我们可以根据自己的需要去增强MyBatis 的功能.需要 ...

  3. IIS6.0(CVE-2017-7269) 缓冲器溢出

    漏洞描述开启WebDAV服务对IIS 6.0存在缓冲区溢出漏洞都可以导致远程代码执行,所以对于目前的IIS 6.0用户而言,可用的变通方案就是关闭WebDAV服务. 漏洞编号CVE-2017-7269 ...

  4. GitHub Actions in Action

    GitHub Actions in Action https://lab.github.com/githubtraining/github-actions:-hello-world https://g ...

  5. js sort map by key

    js sort map by key Map map to array // Array.from() Object let obj = {}; for(let key of Object.keys( ...

  6. webpack-cli bugs All In One

    webpack-cli bugs All In One Error: Cannot find module 'webpack-cli/bin/config-yargs' webpack version ...

  7. Node.js Backend Developer

    Node.js Backend Developer refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  8. js & void() & void(0)

    js & void() & void(0) https://www.runoob.com/js/js-void.html void() <a href="javascr ...

  9. VP9 & AV1 & H.265

    VP9 & AV1 & H.265 视频编码格式 AV1 https://caniuse.com/#search=AV1 VP9 https://caniuse.com/#search ...

  10. javascript & global event & custom event

    javascript & global event & custom event new CustomEvent object let event = new CustomEvent( ...