【Python】执行系统命令的常见用法

简介:
本文总结了使用Python 程序执行系统的几种命令,介绍各个模块的优缺点。
os.system模块

仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息,如果在命令行下执行,结果直接打印出来


Python 2.6.6 (r266:84292, May 29 2014, 05:49:27) 
 
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import os

>>> os.system('cat cont')

biz_order_id

out_order_id

buy_nick

sel_nick

buyer

sel

0

>>> out=os.system('cat cont')

biz_order_id

out_order_id

buy_nick

sel_nick

buyer

sel

>>> 

缺点: 
a.os.system() 是新起一个shell执行命令 
b.不能利用其它变量存放os.system()执行的结果,获得输出等信息比较麻烦.
c.无法控制命令执行的状态,(如果调用的外部命令,挂死或者执行时间很长),主进程无法控制os.system(), 因为调用os.system(cmd) 调用进程会block,直到 os.system()命令结束自己退出。 

os.popen模块
通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出。
例子1


>>> output = os.popen('date')
 
>>> print output

<open file 'date', mode 'r' at 0x7f7ae831e150>

>>> print output.read()

Sun Sep 13 22:34:56 CST 2015 

例子2


>>> output = os.popen('cat cont').readlines() ##返回一个数组
 
>>> print output

['biz_order_id\n', 'out_order_id\n', 'buy_nick\n', 'sel_nick\n', 'buyer\n', 'sel\n']

>>> print output[0]

biz_order_id 

优点
1 该方法不但执行命令还返回执行后的信息对象,将返回的结果赋于一变量,便于程序的处理。
缺点
1 无法获取命令的执行结果的状态。

commands模块
和os.system的执行方式类似,在子系统终端执行命令。该模块包含如下三种函数:
commands.getstatusoutput(cmd)
commands.getoutput(cmd) 返回命令的执行结果
commands.getstatus(file) 返回 ls -ld file 的执行结果。


>>> output=commands.getoutput("date")
 
>>> print output

Sun Sep 13 22:37:44 CST 2015

>>> status=commands.getstatus("date")

>>> print status

ls: cannot access date: No such file or directory

>>> commands.getstatus('/bin/ls')

'-rwxr-xr-x 1 root root 111744 May 29 2014 /bin/ls'

>>> st=commands.getstatus('/bin/ls')

>>> print st

-rwxr-xr-x 1 root root 111744 May 29 2014 /bin/ls

>>> status,output=commands.getstatusoutput("ls yy") 

>>> print status,output

512 ls: cannot access yy: No such file or directory 

优点: 
a.容易获得外部命令的结果输出和命令执行的状态 
缺点: 
b.不能利用其它变量存放os.system()执行的结果,获得输出等信息比较麻烦.
c.无法控制命令执行的状态,(如果调用的外部命令,挂死或者执行时间很长),主进程无法控制os.system(), 因为调用os.system(cmd) 调用进程会block,直到 os.system()命令结束自己退出。 
需要注意事的是 该模块已经在 python 3 中被废弃了,推荐使用 subprocess代替。

subprocess 模块
运行python的时候,我们都是在创建并运行一个进程。像Linux进程那样,一个进程可以fork一个子进程,并让这个子进程exec另外一个程序。在Python中,通过标准库中的subprocess包来fork一个子进程,并运行一个外部的程序。详细资料请参考 官方文档



>>> import subprocess
 
>>> subprocess.call(["ls", "-l"])

total 20

-rwxr-xr-x 1 qilong.yangql users 14 Dec 30 2014 a

-rw-r--r-- 1 qilong.yangql users 54 Sep 13 22:22 cont

-rwxr-xr-x 1 root root 19 Jun 1 11:01 d

-rw-r--r-- 1 root root 68 Jul 7 09:45 database

drwxr-xr-x 3 qilong.yangql users 4096 Jan 11 2015 dbscripts

0

>>> subprocess.call(["ls"]) 

a cont d database dbscripts

0

>>> Popen = subprocess.Popen(["date"])

>>> Sun Sep 13 22:57:08 CST 2015

>>> Popen.pid 

优点
1 支持和子进程交互 
2 支持命令结果输出
3 可以使用kill()  ,terminate() 来控制子进程退出 
4 还有很多请参考官方文档。


相关文章
|
27天前
|
JavaScript 前端开发 关系型数据库
事件管理工具:用Python和Vue打造在线预订和票务系统
【4月更文挑战第11天】构建一个在线预订和票务系统,结合Python(Flask或Django)后端与Vue.js前端。准备工作包括设置Python环境、Node.js、数据库和Git。后端创建RESTful API,Flask适合轻量级,Django提供完整框架。前端使用Vue CLI、Vuex和Vue Router构建用户界面。通过Vuex管理状态,Vue Router定义路由,Axios与后端通信。这种架构支持团队协作,代码维护和扩展。
|
1天前
|
Linux 数据安全/隐私保护 iOS开发
如何将python命令链接到Python3
如何将python命令链接到Python3
5 0
|
1天前
|
运维 监控 Ubuntu
Python实现ubuntu系统进程内存监控
Python实现ubuntu系统进程内存监控
8 1
|
1天前
|
Shell 测试技术 Python
在Mac上用Python调用终端执行命令
在Mac上用Python调用终端执行命令
6 1
|
9天前
|
Python 容器
Python中的for循环用法详解,一文搞定它
Python中的for循环用法详解,一文搞定它
|
9天前
|
弹性计算 运维 Shell
设置Python 支持自动命令补齐功能
【4月更文挑战第29天】
8 0
|
9天前
|
弹性计算 运维 Shell
设置 Python 支持自动命令补齐功能
【4月更文挑战第29天】
7 1
|
13天前
|
数据可视化 Python
Python的分子模拟动态促进DF Theory理论对二进制硬盘系统的适用性
Python的分子模拟动态促进DF Theory理论对二进制硬盘系统的适用性
|
14天前
|
Python
【Python笔记】pip intall -e命令:让你的工程直接使用开源包的源码,可断点调试,修改源码!
【Python笔记】pip intall -e命令:让你的工程直接使用开源包的源码,可断点调试,修改源码!
14 0