python2.X中的commands模块的用法

commands模块是python2.X的内置模块,共有三个函数,使用help(commands)可以查到。
\color{red}{注意}:python2中的commands模块在python3.x被subprocess取代了。

FUNCTIONS
    getoutput(cmd)
        Return output (stdout or stderr) of executing cmd in a shell.
       #返回在shell中执行cmd的输出(stdout或stderr)。
    getstatus(file)
        Return output of "ls -ld <file>" in a string.

    getstatusoutput(cmd)
        Return (status, output) of executing cmd in a shell.

1、 commands.getstatusoutput(cmd)返回一个元组(status,output)
status代表的shell命令的返回状态,如果成功的话是0;output是shell的返回的结果

import commands
status, output = commands.getstatusoutput("ls")
print status # 0
print output 

2、返回ls -ld file 执行的结果。
commands.getstatus(file)
3、判断Shell命令的输出内容
commands.getoutput(cmd)

print commands.getoutput("ls")
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。