游走的鱼

导航

python与系统做交互常用的模块和使用方法

1.使用os模块与系统做简单命令的交互

>>>import os

>>>os.popen('pwd')

<open file 'pwd', mode 'r' at 0x7f6e27b6a420>

>>>a = os.popen('pwd').read()

>>>a

'/root\n'

 

2.使用os.system

>>> import os
>>> os.system('ls')
disk_cleaner.log disk_cleaner.py eslog_clear.log eslog_clear.sh mount.sh senddingtalk.py sendemail.py send_sms.py
0
>>>

 

3.使用commands模块

>>>import commands

>>>res = commands.getstatusoutput('pwd')

>>>res

(0, '/root')

0表示执行结果的状态,非零的话表示执行结果有错误。

posted on 2017-01-18 11:40  游走的鱼  阅读(161)  评论(0编辑  收藏  举报