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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Windows 下更改 jupyterlab 默認(rèn)啟動位置的教程詳解
這篇文章主要介紹了Windows 下更改 jupyterlab 默認(rèn)啟動位置,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
Python實現(xiàn)解析路徑字符串并獲取每個文件夾名稱
在?Python?中,解析路徑字符串并獲取每個文件夾的名稱是一項常見的任務(wù),這篇文章主要為大家詳細介紹了Python解析路徑字符串的具體方法,希望對大家有所幫助2024-04-04
Python數(shù)據(jù)分析庫pandas高級接口dt的使用詳解
這篇文章主要介紹了Python數(shù)據(jù)分析庫pandas高級接口dt的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
詳解win10下pytorch-gpu安裝以及CUDA詳細安裝過程
這篇文章主要介紹了win10下pytorch-gpu安裝以及CUDA詳細安裝過程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

