网络编程
位置:首页>> 网络编程>> Python编程>> python commands模块的适用方式

python commands模块的适用方式

作者:以我丶之姓  发布时间:2022-02-26 19:38:14 

标签: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 &ldquo;ls -ld file&rdquo; in a string.
getstatusoutput(cmd)Return (status, output) of executing cmd in a shell.

来源:https://www.jianshu.com/p/d29b8a2119ad

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com