python-commands模块的适用

来源:互联网 发布:魅族云相册软件 编辑:程序博客网 时间:2024/06/04 13:40

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

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、 commands.getstatusoutput(cmd)返回一个元组(status,output)
status代表的shell命令的返回状态,如果成功的话是0;output是shell的返回的结果

>>> import commands>>> status, output = commands.getstatusoutput("ls")>>> print status0>>> print outputatom:bookstorecookie.py~

2、返回ls -ld file执行的结果.
commands.getstatus(file)

3、判断Shell命令的输出内容
commands.getoutput(cmd)

>>> print commands.getoutput("ls")atom:bookstorecookie.py~
0 0
原创粉丝点击