python commands模塊的適用方式
commands模塊的適用
commands模塊是python的內(nèi)置模塊,他共有三個函數(shù),使用help(commands)可以查看到
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命令的返回狀態(tài),如果成功的話是0;output是shell的返回的結(jié)果
>>> import commands >>> status, output = commands.getstatusoutput("ls") >>> print status 0 >>> print output atom: bookstore cookie.py~
2、返回ls -ld file執(zhí)行的結(jié)果
commands.getstatus(file)
3、判斷Shell命令的輸出內(nèi)容
commands.getoutput(cmd)
>>> print commands.getoutput("ls") atom: bookstore cookie.py~
commands 方法
commands 模塊是 Python 的內(nèi)置模塊,它主要有三個函數(shù):
函數(shù) | 說明 |
---|---|
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. |
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python 模擬員工信息數(shù)據(jù)庫操作的實例
下面小編就為大家?guī)硪黄狿ython 模擬員工信息數(shù)據(jù)庫操作的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10python中必會的四大高級數(shù)據(jù)類型(字符,元組,列表,字典)
這篇文章主要介紹了python中必會的四大高級數(shù)據(jù)類型(字符,元組,列表,字典),本文通過實例圖文相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-05-05Python學(xué)習(xí)筆記之文件的讀寫操作實例分析
這篇文章主要介紹了Python學(xué)習(xí)筆記之文件的讀寫操作,結(jié)合實例形式詳細分析了Python常見的文件讀寫操作實現(xiàn)技巧及相關(guān)注意事項,需要的朋友可以參考下2019-08-08使用CodeMirror實現(xiàn)Python3在線編輯器的示例代碼
這篇文章主要介紹了使用CodeMirror實現(xiàn)Python3在線編輯器的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01python創(chuàng)建學(xué)生成績管理系統(tǒng)
這篇文章主要為大家詳細介紹了python創(chuàng)建學(xué)生成績管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11