1、commands模块只使用与linux的shell模式下

a) commands.getoutput(cmd)

[root@localhost ~]# cat a.py 
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import commands

cmd = 'ls /home/admin'
a = commands.getoutput(cmd)
print(type(a))
print(a)

结果:
[root@localhost ~]# python a.py

[root@localhost ~]# cat c.py 
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import commands
cmd = 'ls /home/admin'
c = commands.getstatusoutput(cmd)
print(type(c))
status, output = commands.getstatusoutput(cmd)
print(status)
print(output)
print(type(output))

结果:
[root@localhost ~]# python c.py

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐