现在的位置: 首页 > 综合 > 正文

python与shell的完美结合

2019年08月05日 ⁄ 综合 ⁄ 共 470字 ⁄ 字号 评论关闭

有时候在python中使用一些shell的功能,其实是非常有必要的。以前自己只知道os.path/stat/glob/shutil等模块,今天发现还有一个模块:commands,用起来非常方面。举例如下:

$ python
>>> import commands
>>> s=commands.getoutput("ps -ef").split('\n')
>>> for i in s:
...     print i
...
     UID     PID    PPID TTY     STIME COMMAND
testuser    2876       1 con  14:17:34 /usr/bin/bash
testuser    3048    2876 con  14:22:07 /usr/bin/python2.4
testuser    1400    3048 con  14:22:42 /usr/bin/sh
testuser    3244    1400 con  14:22:43 /usr/bin/ps
>>>

摘自http://blog.chinaunix.net/u/23216/showart_193005.html

 

ps: 在python2.6下有这个模块,但python3.2下默认没有

抱歉!评论已关闭.