好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

python commands模块的适用方式

commands模块的适用

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 status
0
>>> print output
atom:
bookstore
cookie.py~

2、返回ls -ld file执行的结果

commands.getstatus(file)

3、判断Shell命令的输出内容

commands.getoutput(cmd)

>>> 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.

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

查看更多关于python commands模块的适用方式的详细内容...

  阅读:69次