您的位置:首页 > 编程语言 > Python开发

Python资料之commands模块

2018-01-15 14:34 531 查看
commands模块是python的内置模块,他共有三个函数,使用help(commands)可以查看到。

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

FUNCTIONS
getoutput(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: