侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Python如何调用外部系统命令

Python  /  管理员 发布于 7年前   207

 前言

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

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

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

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 subprocessp = subprocess.Popen(args='mkdir test', shell=True, cwd='d:/temp')p.wait()

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

import subprocessp = 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 subprocessretcode = 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 subprocessoutput = 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 osretcode = 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 osoutput = 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 commandsretcode, output = commands.getstatusoutput('ping -n 2 -w 3 192.168.1.104')print retcodeprint output

总结

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

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


  • 上一条:
    Python在cmd上打印彩色文字实现过程详解
    下一条:
    python 使用socket传输图片视频等文件的实现方式
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客