脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python commands模块的适用方式

python commands模块的适用方式

2022-09-23 10:06以我丶之姓 Python

这篇文章主要介绍了python commands模块的适用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

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

延伸 · 阅读

精彩推荐