学无先后,达者为师

网站首页 编程语言 正文

python commands模块的适用方式_python

作者:以我丶之姓   更新时间: 2022-04-18 编程语言

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.

原文链接:https://www.jianshu.com/p/d29b8a2119ad

栏目分类
最近更新