commands模块python的内置模块,他共有三个函数,使用help(commands)可以查看到。

注:在3.x版本总,getstatus()方法被移除,getoutput()和getstatusoutput()被放到了subprocess模块中。

FUNCTIONSgetoutput(cmd)Return output (stdout or stderr) of executing cmd in a shell.getstatus(file)Return output of "ls -ld <file>" in a string.getstatusoutput(cmd)Return (status, output) of executing cmd in a shell.

1、getoutput(cmd):
执行cmd命令,并返回输出的内容,返回结果为str。

>>> import commands
>>> out=commands.getoutput("ls -l")
>>> print(out)
-r-xr-x--- 1 serv service 377 115 16:06 free.py

2、getstatus(file):
返回执行ls -ld file命令的结果。该函数已被python丢弃,不建议使用。

3、getstatusoutput(cmd)
执行cmd命令,并返回执行的状态和输出的内容,返回结果为int和str。

>>> import commands
>>> status,out=commands.getstatusoutput("ls -l")
>>> print(status)
0
>>> print(out)
-r-xr-x--- 1 serv service 377 115 16:06 free.py