在Python中执行系统命令的方法示例详解

作者:xl365t 时间:2022-10-24 05:32:27 

前言

Python经常被称作“胶水语言”,因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库。在Python/wxPython环境下,执行外部命令或者说在Python程序中启动另一个程序的方法。

本文将详细介绍关于Python中如何执行系统命令的相关资料,下面话不多说了,来一起看看详细的介绍吧。

(1) os.system()

这个方法直接调用标准C的system()函数,仅仅在一个子终端运行系统命令,而不能获取执行返回的信息。


>>> import os
>>> output = os.system('cat /proc/cpuinfo')
processor : 0
vendor_id : AuthenticAMD
cpu family : 21
... ...
>>> output # doesn't capture output
0

(2) os.popen()

这个方法执行命令并返回执行后的信息对象,是通过一个管道文件将结果返回。


>>> output = os.popen('cat /proc/cpuinfo')
>>> output
<open file 'cat /proc/cpuinfo', mode 'r' at 0x7ff52d831540>
>>> print output.read()
processor : 0
vendor_id : AuthenticAMD
cpu family : 21
... ...
>>><span style="font-size:14px;">

(3) commands模块


>>> import commands
>>> (status, output) = commands.getstatusoutput('cat /proc/cpuinfo')
>>> print output
processor : 0
vendor_id : AuthenticAMD
cpu family : 21
... ...
>>> print status
0

注意1:在类unix的系统下使用此方法返回的返回值(status)与脚本或命令执行之后的返回值不等,这是因为调用了os.wait()的缘故,具体原因就得去了解下系统wait()的实现了。需要正确的返回值(status),只需要对返回值进行右移8位操作就可以了。

注意2:当执行命令的参数或者返回中包含了中文文字,那么建议使用subprocess。

(4) subprocess模块

该模块是一个功能强大的子进程管理模块,是替换os.system, os.spawn*等方法的一个模块。


>>> import subprocess
>>> subprocess.Popen(["ls", "-l"]) <strong> # python2.x</strong> doesn't capture output
>>> subprocess.run(["ls", "-l"]) <strong># python3.x</strong> doesn't capture output
<subprocess.Popen object at 0x7ff52d7ee490>
>>> total 68
drwxrwxr-x 3 xl xl 4096 Feb 8 05:00 com
drwxr-xr-x 2 xl xl 4096 Jan 21 02:58 Desktop
drwxr-xr-x 2 xl xl 4096 Jan 21 02:58 Documents
drwxr-xr-x 2 xl xl 4096 Jan 21 07:44 Downloads
... ...
>>>

总结

补充:

最近在做那个测试框架的时候发现 Python 的另一个获得系统执行命令的返回值和输出的类。

最开始的时候用 Python 学会了 os.system() 这个方法是很多比如 C,Perl 相似的。

os.system('cat /proc/cpuinfo')

但是这样是无法获得到输出和返回值的,继续 Google,之后学会了 os.popen()。

output = os.popen('cat /proc/cpuinfo')
print output.read()

通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出。但是怎么读取程序执行的返回值呢,当然咯继续请教伟大的 Google(联想到像我这样的人工作如果离开了 Google,不是成了废物。。。Baidu 忽视)。Google 给我指向了 commands — Utilities for running commands。
这样通过 commands.getstatusoutput() 一个方法就可以获得到返回值和输出,非常好用。

(status, output) = commands.getstatusoutput('cat /proc/cpuinfo')
print status, output

Python Document 中给的一个例子,很清楚的给出了各方法的返回。

>>> import commands
>>> commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
>>> commands.getstatusoutput('cat /bin/junk')
(256, 'cat: /bin/junk: No such file or directory')
>>> commands.getstatusoutput('/bin/junk')
(256, 'sh: /bin/junk: not found')
>>> commands.getoutput('ls /bin/ls')
'/bin/ls'
>>> commands.getstatus('/bin/ls')
'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'

来源:http://blog.csdn.net/u010318270/article/details/56841900

标签:python,执行,系统命令
0
投稿

猜你喜欢

  • python操作excel的包(openpyxl、xlsxwriter)

    2023-05-22 09:12:07
  • python远程连接MySQL数据库

    2024-01-19 13:53:29
  • 详解Golang语言HTTP客户端实践

    2023-09-17 13:52:07
  • PHP生成网站桌面快捷方式代码分享

    2023-06-16 02:54:17
  • python机器学习pytorch 张量基础教程

    2023-06-18 04:54:31
  • SqlServer中的日期与时间函数

    2011-11-03 17:12:34
  • Python+Selenium+phantomjs实现网页模拟登录和截图功能(windows环境)

    2023-11-17 11:00:43
  • Python中最好用的json库orjson用法详解

    2023-06-13 23:11:49
  • Python Web框架之Django框架cookie和session用法分析

    2021-08-23 05:30:15
  • PHP排序算法之冒泡排序(Bubble Sort)实现方法详解

    2024-05-13 09:25:52
  • 让复杂导航设计变得简单

    2008-01-07 11:50:00
  • MySQL redo死锁问题排查及解决过程分析

    2024-01-17 08:17:02
  • Oracle如何直接运行OS命令(上)第1/2页

    2010-07-30 12:54:00
  • 手写Vue2.0 数据劫持的示例

    2024-05-22 10:43:17
  • 关于前端文件下载各类方式大汇总

    2024-06-13 03:42:01
  • python 发送和接收ActiveMQ消息的实例

    2022-08-23 18:45:20
  • js中的文档模式-document.compatMode

    2009-08-14 20:30:00
  • Python面向对象程序设计OOP深入分析【构造函数,组合类,工具类等】

    2022-06-01 04:19:02
  • JavaScript设计模式之单例模式

    2024-04-29 14:08:39
  • 浅谈python已知元素,获取元素索引(numpy,pandas)

    2023-08-04 16:01:00
  • asp之家 网络编程 m.aspxhome.com