Python如何调用外部系统命令


Posted in Python onAugust 07, 2019

 前言

利用Python调用外部系统命令的方法可以提高编码效率。调用外部系统命令完成后可以通过获取命令执行返回结果码、执行的输出结果进行进一步的处理。本文主要描述Python常见的调用外部系统命令的方法,包括os.system()、os.popen()、subprocess.Popen()等。

本文分析python调用外部系统命令主要从两个方面考虑:1、是不是可以返回命令执行结果码,因为大部分场景都需要通过判断调用命令是执行成功还是失败。2、是不是可以获取命令执行结果。某些场景调用外部命令就是为获取输出结果,也可以通过输出结果来判断命令执行成功还是失败。分析结果如下:

Python如何调用外部系统命令

下面再针对每一个函数使用方法和实例进行详细描述。

1、subprocess模块

优先介绍subprocess模块的是由于该模块可以替代旧模块的方法,如os.system()、os.popen()等,推荐使用。subporcess模块可以调用外部系统命令来创建新子进程,同时可以连接到子进程的nput/output/error管道上,并得到子进程的返回值。subprocess模块主要有call()、check_call()、check_output()、Popen()函数,简要描述如下:

Main API
  ========
  call(...): Runs a command, waits for it to complete, then returns the return code.
  check_call(...): Same as call() but raises CalledProcessError() if return code is not 0
  check_output(...): Same as check_call() but returns the contents of stdout instead of a return code
  Popen(...): A class for flexibly executing a command in a new process
  
  Constants
  ---------
  PIPE:  Special value that indicates a pipe should be created
  STDOUT: Special value that indicates that stderr should go to stdout

下面开始介绍subprocess函数的使用方法。

(1)subprocess.Popen类

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:
  要调用的外部系统命令。
 bufsize:
  默认值为0, 表示不缓存,。为1表示行缓存,。其他正数表示缓存使用的大小,,负数-1表示使用系统默认的缓存大小。
 stdin、stdout、stdout
  分别表示标准输入、标准输出和标准错误。其值可以为PIPE、文件描述符和None等。默认值为None,表示从父进程继承。
 shell
  Linux:参数值为False时,Linux上通过调用os.execvp执行对应的程序。为Trule时,Linux上直接调用系统shell来执行程序。
  Windows:shell参数表示是否使用bat作为执行环境。只有执行windows的dir、copy等命令时才需要设置为True。其他程序没有区别。
 executable
  用于指定可执行程序。一般情况下我们通过args参数来设置所要运行的程序。如果将参数shell设为 True,executable将指定程序使用的shell。在windows平台下,默认的shell由COMSPEC环境变量来指定。
 preexec_fn
  只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用
 cwd
 设置子进程当前目录
 env
  env是字典类型,用于指定子进程的环境变量。默认值为None,表示子进程的环境变量将从父进程中继承。
 Universal_newlines
  不同操作系统下,文本的换行符是不一样的。如:windows下用'/r/n'表示换,而Linux下用 ‘/n'。如果将此参数设置为True,Python统一把这些换行符当作'/n'来处理。

Popen对象对应的属性和方法如下:

属性:
 stdin, stdout, stderr, pid, returncode
方法:
 communicate(self, input=None) -> returns a tuple (stdout, stderr).
 wait(self) -> Wait for child process to terminate. Returns returncode attribute.

常用实例

1、打印D:\temp目录下创建test目录。直接调用进程,不考虑获取调用命令输出内容和结果码

import subprocess
p = subprocess.Popen(args='mkdir test', shell=True, cwd='d:/temp')
p.wait()

2、调用ping命令执行,获取命令执行输出内容

import subprocess
p = subprocess.Popen(args='ping -n 2 -w 3 192.168.1.104', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
p.wait()
print p.stdout.read()

说明:p.stdout、p.stdin、p.stderr为文件对象,可以使用文件对象函数,如read()。

(2)subprocess.call()

函数原型:call(*popenargs, **kwargs)。call()调用外部系统命令执行,并返回程序执行结果码。

import subprocess
retcode = subprocess.call('ping -n 2 -w 3 192.168.1.104', shell=True)
print retcode

(3)subprocess.check_call()

使用方法同call()。如果调用命令执行成功,返回结果码0,如果执行失败,抛出CalledProcessError.异常。举例如下:

>>> p = subprocess.check_call('ping -n 2 -w 3 192.168.1.105', shell=True)
正在 Ping 192.168.1.105 具有 32 字节的数据:
请求超时。
请求超时。
192.168.1.105 的 Ping 统计信息:
  数据包: 已发送 = 2,已接收 = 0,丢失 = 2 (100% 丢失),
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "c:\Python27\lib\subprocess.py", line 186, in check_call
  raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command 'ping -n 2 -w 3 192.168.1.105' returned non-zero exit status 1

(4)subprocess.check_output()

函数原型:check_output(*popenargs, **kwargs)。用法与call()相同。区别是如果执行成功返回的是标准输出内容。如果失败,抛CalledProcessError.异常。

import subprocess
output = subprocess.check_output('ping -n 2 -w 3 192.168.1.104', shell=True)
print output

2、os模块

(1)os.system()

os.system(command) 。调用外部系统命令,返回命令结果码,但是无法获取命令执行输出结果,输出结果直接打印到屏幕终端。

import os
retcode = os.system('ping -n 2 -w 3 192.168.1.104')
if retcode == 0:
  print "%s Success" % (ip,)
else:
  print "%s Fail" % (ip,)

(2)os.popen()

os.popen(command) 。调用外部系统命令,返回命令执行输出结果,但不返回结果吗

import os
output = os.popen('ping -n 2 -w 3 192.168.1.104')
print output

3、commands模块

commands模块用于调用Linux shell命令。测试了下在windows上执行失败。主要有如下3个函数

getoutput(cmd): Return output (stdout or stderr) of executing cmd in a shell.
getstatus(file):Return output of "ls -ld <file>" in a string.
getstatusoutput(cmd):Return (status, output) of executing cmd in a shell

使用实例如下:

import commands
retcode, output = commands.getstatusoutput('ping -n 2 -w 3 192.168.1.104')
print retcode
print output

总结

在编写程序时可根据使用场景来选择不同的Python调用方法来执行外部系统命令。对于复杂的命令考虑使用subprocess.Popen()完成,如果仅是简单的命令执行,可以使用os.system()完成,如调用windows的暂停程序命令os.system('pause')。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中实现对Timestamp和Datetime及UTC时间之间的转换
Apr 08 Python
Python 爬虫爬取指定博客的所有文章
Feb 17 Python
关于pip的安装,更新,卸载模块以及使用方法(详解)
May 19 Python
python编程培训 python培训靠谱吗
Jan 17 Python
Python实现PS图像抽象画风效果的方法
Jan 23 Python
python 去除txt文本中的空格、数字、特定字母等方法
Jul 24 Python
python+mysql实现学生信息查询系统
Feb 21 Python
解决pycharm启动后总是不停的updating indices...indexing的问题
Nov 27 Python
Python 发送邮件方法总结
Aug 10 Python
pip 20.3 新版本发布!即将抛弃 Python 2.x(推荐)
Dec 16 Python
python编写扎金花小程序的实例代码
Feb 23 Python
python正则表达式re.search()的基本使用教程
May 21 Python
PyQt5通信机制 信号与槽详解
Aug 07 #Python
python 使用socket传输图片视频等文件的实现方式
Aug 07 #Python
python获取Pandas列名的几种方法
Aug 07 #Python
python 提取文件指定列的方法示例
Aug 07 #Python
PyQt Qt Designer工具的布局管理详解
Aug 07 #Python
Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str
Aug 07 #Python
python利用7z批量解压rar的实现
Aug 07 #Python
You might like
PHILIPS L4X25T电路分析和打理
2021/03/02 无线电
php面向对象全攻略 (十七) 自动加载类
2009/09/30 PHP
php对数组排序的简单实例
2013/12/25 PHP
jQuery获取json后使用zy_tmpl生成下拉菜单
2015/03/27 PHP
Laravel构建即时应用的一种实现方法详解
2017/08/31 PHP
javascript比较文档位置
2008/04/08 Javascript
用js实现table单元格高宽调整,兼容合并单元格(兼容IE6、7、8、FF)实例
2013/06/25 Javascript
javascript列表框操作函数集合汇总
2013/11/28 Javascript
js/jquery解析json和数组格式的方法详解
2014/01/09 Javascript
利用jQuery实现可以编辑的表格
2014/05/26 Javascript
一个很有趣3D球状标签云兼容IE8
2014/08/22 Javascript
Javascript中使用A标签获取当前目录的绝对路径方法
2015/03/02 Javascript
Jqprint实现页面打印
2017/01/06 Javascript
详解nodejs微信公众号开发——4.自动回复各种消息
2017/04/11 NodeJs
分享5个小技巧让你写出更好的 JavaScript 条件语句
2018/10/20 Javascript
Vue一次性简洁明了引入所有公共组件的方法
2018/11/28 Javascript
如何为vue的项目添加单元测试
2018/12/19 Javascript
Vue的props父传子的示例代码
2020/05/20 Javascript
深入解读VUE中的异步渲染的实现
2020/06/19 Javascript
[02:14]2016国际邀请赛中国区预选赛Ehome晋级之路
2016/07/01 DOTA
Python的Tornado框架实现图片上传及图片大小修改功能
2016/06/30 Python
Python中列表和元组的使用方法和区别详解
2020/12/30 Python
python图像常规操作
2017/11/11 Python
运动检测ViBe算法python实现代码
2018/01/09 Python
python验证码识别教程之利用滴水算法分割图片
2018/06/05 Python
使用python实现http及ftp服务进行数据传输的方法
2018/10/26 Python
python 删除字符串中连续多个空格并保留一个的方法
2018/12/22 Python
Django Rest framework三种分页方式详解
2019/07/26 Python
python每天定时运行某程序代码
2019/08/16 Python
Python:__eq__和__str__函数的使用示例
2020/09/26 Python
利用Python如何画一颗心、小人发射爱心
2021/02/21 Python
英国布鲁姆精品店:Bloom Boutique
2018/03/01 全球购物
企业管理培训感言
2014/01/27 职场文书
外贸英文求职信范文
2015/03/19 职场文书
Redis入门基础常用操作命令整理
2022/06/01 Redis
微软发布Windows 11今年最大更新22H2(附 ISO 镜像官方下载)
2022/09/23 数码科技