python commands 模块


commands 模块

通过python调用系统命令 只适用于linux
commands是提供linux系统环境下支持使用shell命令的一个模块

commands.getoutput(cmd)

只返回执行shell命令的结果

import commands

cmd = 'ls /opt'

a = commands.getoutput(cmd)

print(type(a))
print(a)


# <type 'str'>
# auto-mongo-primary.sh
# install-zabbix.sh
# python.py 
# soft


commands.getstatusoutput(cmd)

import commands
cmd = 'ls /home/admin'
c = commands.getstatusoutput(cmd)
print(type(c))
status, output = commands.getstatusoutput(cmd)
print(status)
print(output)
print(type(output))

# <type 'str'>
# 0
# auto-mongo-primary.sh
# install-zabbix.sh
# python.py 
# soft

返回结果是一个tuple,第一个值是shell执行的结果,如果shell执行成功,返回0,否则,为非0,第二个是一个字符串,就是我们shell命令的执行结果,python通过一一对应的方式复制给status和output


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM