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

转载于:https://www.cnblogs.com/lijunjiang2015/p/7816016.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值