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

简介: 本文总结了使用Python 程序执行系统的几种命令,介绍各个模块的优缺点。os.system模块 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息,如果在命令行下执行,结果直接打印出来 Python 2.
本文总结了使用Python 程序执行系统的几种命令,介绍各个模块的优缺点。
os.system模块
仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息,如果在命令行下执行,结果直接打印出来
  1. Python 2.6.6 (r266:84292, May 29 2014, 05:49:27)
  2. [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> import os
  5. >>> os.system('cat cont')
  6. biz_order_id
  7. out_order_id
  8. buy_nick
  9. sel_nick
  10. buyer
  11. sel
  12. 0
  13. >>> out=os.system('cat cont')
  14. biz_order_id
  15. out_order_id
  16. buy_nick
  17. sel_nick
  18. buyer
  19. sel
  20. >>>
缺点: 
a.os.system() 是新起一个shell执行命令 
b.不能利用其它变量存放os.system()执行的结果,获得输出等信息比较麻烦.
c.无法控制命令执行的状态,(如果调用的外部命令,挂死或者执行时间很长),主进程无法控制os.system(), 因为调用os.system(cmd) 调用进程会block,直到 os.system()命令结束自己退出。 

os.popen模块
通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出。
例子1
  1. >>> output = os.popen('date')
  2. >>> print output
  3. <open file 'date', mode 'r' at 0x7f7ae831e150>
  4. >>> print output.read()
  5. Sun Sep 13 22:34:56 CST 2015
例子2
  1. >>> output = os.popen('cat cont').readlines() ##返回一个数组
  2. >>> print output
  3. ['biz_order_id\n', 'out_order_id\n', 'buy_nick\n', 'sel_nick\n', 'buyer\n', 'sel\n']
  4. >>> print output[0]
  5. biz_order_id
优点
1 该方法不但执行命令还返回执行后的信息对象,将返回的结果赋于一变量,便于程序的处理。
缺点
1 无法获取命令的执行结果的状态。

commands模块
和os.system的执行方式类似,在子系统终端执行命令。该模块包含如下三种函数:
commands.getstatusoutput(cmd)
commands.getoutput(cmd) 返回命令的执行结果
commands.getstatus(file) 返回 ls -ld file 的执行结果。
  1. >>> output=commands.getoutput("date")
  2. >>> print output
  3. Sun Sep 13 22:37:44 CST 2015
  4. >>> status=commands.getstatus("date")
  5. >>> print status
  6. ls: cannot access date: No such file or directory
  7. >>> commands.getstatus('/bin/ls')
  8. '-rwxr-xr-x 1 root root 111744 May 29 2014 /bin/ls'
  9. >>> st=commands.getstatus('/bin/ls')
  10. >>> print st
  11. -rwxr-xr-x 1 root root 111744 May 29 2014 /bin/ls
  12. >>> status,output=commands.getstatusoutput("ls yy")
  13. >>> print status,output
  14. 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一个子进程,并运行一个外部的程序。详细资料请参考 官方文档
  1. >>> import subprocess
  2. >>> subprocess.call(["ls", "-l"])
  3. total 20
  4. -rwxr-xr-x 1 qilong.yangql users 14 Dec 30 2014 a
  5. -rw-r--r-- 1 qilong.yangql users 54 Sep 13 22:22 cont
  6. -rwxr-xr-x 1 root root 19 Jun 1 11:01 d
  7. -rw-r--r-- 1 root root 68 Jul 7 09:45 database
  8. drwxr-xr-x 3 qilong.yangql users 4096 Jan 11 2015 dbscripts
  9. 0
  10. >>> subprocess.call(["ls"])
  11. a cont d database dbscripts
  12. 0
  13. >>> Popen = subprocess.Popen(["date"])
  14. >>> Sun Sep 13 22:57:08 CST 2015
  15. >>> Popen.pid
优点
1 支持和子进程交互 
2 支持命令结果输出
3 可以使用kill()  ,terminate() 来控制子进程退出 
4 还有很多请参考官方文档。
目录
相关文章
|
4月前
|
Python
python封装执行cmd命令的方法
python封装执行cmd命令的方法
40 0
|
6月前
|
Shell 数据处理 Python
Python 运行 shell 命令的一些方法
Python 运行 shell 命令的一些方法
|
8月前
|
Shell Linux Python
Crontab 执行 python 脚本不生效?
Crontab 执行 python 脚本不生效?
|
监控 Shell Linux
Python调用系统命令的六种方法
Python调用系统命令的六种方法
205 0
|
Python
Python - 执行cmd命令
Python - 执行cmd命令
1805 0