commands模块的适用
commands模块是python的内置模块,他共有三个函数,使用help(commands)可以查看到
1
2
3
4
5
6
7
|
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的返回的结果
1
2
3
4
5
6
7
8
|
>>> import commands >>> status, output = commands.getstatusoutput( "ls" ) >>> print status 0 >>> print output atom: bookstore cookie.py~ |
2、返回ls -ld file执行的结果
1
|
commands.getstatus( file ) |
3、判断Shell命令的输出内容
1
|
commands.getoutput(cmd) |
1
2
3
4
|
>>> print commands.getoutput( "ls" ) atom: bookstore cookie.py~ |
commands 方法
commands 模块是 Python 的内置模块,它主要有三个函数:
函数 | 说明 |
---|---|
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. |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://www.jianshu.com/p/d29b8a2119ad