python commands模塊的適用方式
更新時間:2022年02月11日 16:03:56 作者:以我丶之姓
這篇文章主要介紹了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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python 模擬員工信息數(shù)據(jù)庫操作的實例
下面小編就為大家?guī)硪黄狿ython 模擬員工信息數(shù)據(jù)庫操作的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
python中必會的四大高級數(shù)據(jù)類型(字符,元組,列表,字典)
這篇文章主要介紹了python中必會的四大高級數(shù)據(jù)類型(字符,元組,列表,字典),本文通過實例圖文相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-05-05
使用CodeMirror實現(xiàn)Python3在線編輯器的示例代碼
這篇文章主要介紹了使用CodeMirror實現(xiàn)Python3在線編輯器的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-01-01
python創(chuàng)建學生成績管理系統(tǒng)
這篇文章主要為大家詳細介紹了python創(chuàng)建學生成績管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11

