python commands模塊的適用方式
commands模塊的適用
commands模塊是python的內(nèi)置模塊,他共有三個(gè)函數(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)返回一個(gè)元組(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)置模塊,它主要有三個(gè)函數(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. |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Windows 下更改 jupyterlab 默認(rèn)啟動(dòng)位置的教程詳解
這篇文章主要介紹了Windows 下更改 jupyterlab 默認(rèn)啟動(dòng)位置,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Python實(shí)現(xiàn)解析路徑字符串并獲取每個(gè)文件夾名稱
在?Python?中,解析路徑字符串并獲取每個(gè)文件夾的名稱是一項(xiàng)常見的任務(wù),這篇文章主要為大家詳細(xì)介紹了Python解析路徑字符串的具體方法,希望對(duì)大家有所幫助2024-04-04
五個(gè)方便好用的Python自動(dòng)化辦公腳本的實(shí)現(xiàn)
在沒有人工干預(yù)的情況下,機(jī)器可以自己完成各項(xiàng)任務(wù),這大大提升了工作效率。Python因?yàn)檎Z法簡潔易懂,而且有豐富的第三方工具庫,所以非常適合編寫自動(dòng)化腳本,本文就為大家?guī)砹宋鍌€(gè)這樣的腳本,需要的可以參考一下2022-01-01
Python數(shù)據(jù)分析庫pandas高級(jí)接口dt的使用詳解
這篇文章主要介紹了Python數(shù)據(jù)分析庫pandas高級(jí)接口dt的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12
詳解win10下pytorch-gpu安裝以及CUDA詳細(xì)安裝過程
這篇文章主要介紹了win10下pytorch-gpu安裝以及CUDA詳細(xì)安裝過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

