怎么樣在shell腳本中調(diào)用python腳本?
1、os.system(cmd)
缺點(diǎn):不能獲取返回值
2、os.popen(cmd)
要得到命令的輸出內(nèi)容,只需再調(diào)用下read()或readlines()等
例:a=os.popen(cmd).read()
3、commands模塊,其實(shí)也是對(duì)popen的封裝。
此模塊主要有如下方法:
commands.getstatusoutput(cmd)返回(status, output).
commands.getoutput(cmd)只返回輸出結(jié)果
commands.getstatus(file)返回ls -ld file的執(zhí)行結(jié)果字符串,調(diào)用了getoutput
例:
>>> import commands
>>> commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
>>> commands.getstatusoutput('cat /bin/junk')
(256, 'cat: /bin/junk: No such file or directory')
>>> commands.getstatusoutput('/bin/junk')
(256, 'sh: /bin/junk: not found')
>>> commands.getoutput('ls /bin/ls')
'/bin/ls'
>>> commands.getstatus('/bin/ls')
'-rwxr-xr-x1 root13352 Oct 141994 /bin/ls'
來(lái)源:麥子學(xué)院