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