Python执行系统命令的方法
Python中执行系统命令常见方法有两种:
两者均需 import os
(1) os.system
# 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息
system(command) -> exit_status
Execute the command (a string) in a subshell.
# 如果再命令行下执行,结果直接打印出来
1 |
>>> os.system('ls') |
2 |
04101419778.CHM bash document media py-django video |
3 |
11.wmv books downloads Pictures python |
4 |
all-20061022 Desktop Examples project tools |
(2) os.popen
# 该方法不但执行命令还返回执行后的信息对象
popen(command [, mode='r' [, bufsize]]) -> pipe
Open a pipe to/from a command returning a file object.
例如:
01 |
>>>tmp = os.popen('ls *.py').readlines() |
02 |
>>>tmp |
03 |
Out[21]: |
04 |
['dump_db_pickle.py ', |
05 |
'dump_db_pickle_recs.py ', |
06 |
'dump_db_shelve.py ', |
07 |
'initdata.py ', |
08 |
'__init__.py ', |
09 |
'make_db_pickle.py ', |
10 |
'make_db_pickle_recs.py ', |
11 |
'make_db_shelve.py ', |
12 |
'peopleinteract_query.py ', |
13 |
'reader.py ', |
14 |
'testargv.py ', |
15 |
'teststreams.py ', |
16 |
'update_db_pickle.py ', |
17 |
'writer.py '] |
好处在于:将返回的结果赋于一变量,便于程序的处理。
(3) 使用模块subprocess
1 |
>>> import subprocess |
2 |
>>> subprocess.call (["cmd", "arg1", "arg2"],shell=True) |
获取返回和输出:
1 |
import subprocess |
2 |
p = subprocess.Popen('ls', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
3 |
for line in p.stdout.readlines(): |
4 |
print line, |
5 |
retval = p.wait() |
(4) 使用模块commands模块
1 |
>>> import commands |
2 |
>>> dir(commands) |
3 |
['__all__', '__builtins__', '__doc__', '__file__', '__name__', 'getoutput', 'getstatus','getstatusoutput', 'mk2arg', 'mkarg'] |
4 |
>>> commands.getoutput("date") |
5 |
'Wed Jun 10 19:39:57 CST 2009' |
6 |
>>> |
7 |
>>> commands.getstatusoutput("date") |
8 |
(0, 'Wed Jun 10 19:40:41 CST 2009') |
注意: 当执行命令的参数或者返回中包含了中文文字,那么建议使用subprocess,如果使用os.popen则会出现下面的错误:
1 |
Traceback (most recent call last): |
2 |
File "./test1.py", line 56, in <module> |
3 |
main() |
4 |
File "./test1.py", line 45, in main |
5 |
fax.sendFax() |
6 |
File "./mailfax/Fax.py", line 13, in sendFax |
7 |
os.popen(cmd) |
8 |
UnicodeEncodeError: 'ascii' codec can't encode characters in position 46-52: ordinal not inrange(128) |
关于本文更多的延伸阅读地址:
http://zh-cn.how-to.mobi/index.php?id=89228
Python执行系统命令的方法的更多相关文章
- Python执行系统命令的方法 os.system(),os.popen(),commands
os.popen():用python执行shell的命令,并且返回了结果,括号中是写shell命令 Python执行系统命令的方法: https://my.oschina.net/renwofei42 ...
- 转 Python执行系统命令的方法
传送门 Python执行系统命令的方法 http://www.linux-field.com/?p=15 Python中执行系统命令常见方法有两种: 两者均需 import os (1) os.sys ...
- Python执行系统命令并获得输出的几种方法
[root@a upfc]# ./ffmpeg-linux64-v3.3.1 -i a.mp3 ffmpeg version N-86111-ga441aa90e8-static http://joh ...
- python 执行系统命令模块比较
python 执行系统命令模块比较 1.os.system模块 仅仅在子终端运行命令,返回状态码,0为成功,其他为失败,但是不返回执行结果 如果再命令行下执行,结果直接打印出来 >>> ...
- 提高python执行效率的方法
python上手很容易,但是在使用过程中,怎么才能使效率变高呢? 下面说一下提高python执行效率的方法,这里只是说一点,python在引入模块过程中提高效率的方法. 例如: 1.我们要使用os模块 ...
- windows linux 使用python执行系统命令并将结果保存到变量
最近需要用到os.system 发现不能赋值到变量 后查有更新的模块,如下: os.system os.spawn* os.popen* popen2.* commands.* 重新使用content ...
- python执行系统命令后获取返回值的几种方式集合
python执行系统命令后获取返回值的几种方式集合 今天小编就为大家分享一篇python执行系统命令后获取返回值的几种方式集合,具有很好的参考价值,希望对大家有所帮助.一起跟随小编过来看看吧 第一种情 ...
- Python执行系统命令:使用subprocess的Popen函数
使用subprocess的Popen函数执行系统命令 参考: http://blog.sina.com.cn/s/blog_8f01450601017dlr.html http://blog.csdn ...
- 使用python执行系统命令——subprocess
背景:subprocess是python官方推荐调用系统命令的模块 import subprocess subprocess最主要的两个方法/类: # 参数说明:stdin和stdout相当于一个管 ...
随机推荐
- Jmeter_初步认识随笔
1. 简介 Apache JMeter是100%纯java桌面应用程序,被设计用来测试客户端/服务器结构的软件(例如web应用程序).它可以用来测试包括基于静态和动态资源程序的性能,例如静态文件,Ja ...
- UI:触摸事件 与 事件的回应
事件分类:晃动.触摸.远程控制(如遥控器.红外控制) 触摸开始时候的方法(判断单击,双击,三击事件可以写在这里) -(void)touchesBegan:(NSSet *)touches withEv ...
- Apache Kafka:下一代分布式消息系统
[http://www.infoq.com/cn/articles/apache-kafka/]分布式发布-订阅消息系统. Kafka是一种快速.可扩展的.设计内在就是分布式的,分区的和可复制的提交日 ...
- uva331 - Mapping the Swaps
Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the ...
- 初步认识pg_control文件之一
这个据说是PostgreSQL的control file. 到底如何呢,先看看改名后如何,把pg_control文件改名,然后启动 Postgres,运行时得到信息: [postgres@pg101 ...
- Ajax的常用框架有哪些?
AJAX(Asynchronous JavaScript and XML,异步JavaScript和XML),是创建交互式Web应用的主要开发技术.互联网中也有大量的关于AJAX的框架,本文汇总了最常 ...
- [Angular2 Router] Configuring a Home Route and Fallback Route - Learn An Essential Routing Concept
In this tutorial we are going to learn how to configure the Angular 2 router to cover some commonly ...
- [Angular 2] Order Dynamic Components Inside an Angular 2 ViewContainer
By default, when you generate components, they will simply be added to the page in order, one after ...
- C++11 之for 新解
前言 C++11这次的更新带来了令非常多C++程序猿期待已久的for range循环,每次看到javascript. lua里的for range.心想要是C++能有多好,心里别提多酸了.这 ...
- NBUT 1225 NEW RDSP MODE I
找出循环周期即可了 #include<bits/stdc++.h> using namespace std; int N,M,X; int time(int x,int y,int z) ...