用Python 執(zhí)行cmd命令
更新時間:2020年12月18日 17:09:43 作者:小菠蘿測試筆記
這篇文章主要介紹了用Python 執(zhí)行cmd命令的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
我們通??梢允褂胦s模塊的命令進行執(zhí)行cmd
方法一:os.system
os.system(執(zhí)行的命令) # 源碼 def system(*args, **kwargs): # real signature unknown """ Execute the command in a subshell. """ pass
方法二:os.popen(執(zhí)行的命令)
os.popen(執(zhí)行的命令) # 源碼 def popen(cmd, mode="r", buffering=-1): if not isinstance(cmd, str): raise TypeError("invalid cmd type (%s, expected string)" % type(cmd)) if mode not in ("r", "w"): raise ValueError("invalid mode %r" % mode) if buffering == 0 or buffering is None: raise ValueError("popen() does not support unbuffered streams") import subprocess, io if mode == "r": proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, bufsize=buffering) return _wrap_close(io.TextIOWrapper(proc.stdout), proc) else: proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, bufsize=buffering) return _wrap_close(io.TextIOWrapper(proc.stdin), proc)
兩者區(qū)別
- system只把能輸入的內(nèi)容給返回回來了,其中代碼 0 表示執(zhí)行成功。但是我們沒有辦法獲取輸出的信息內(nèi)容
- popen可以獲取輸出的信息內(nèi)容,它是一個對象,可以通過 .read() 去讀取
以上就是用Python 執(zhí)行cmd命令的詳細內(nèi)容,更多關(guān)于python 執(zhí)行cmd命令的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
淺談Python數(shù)據(jù)類型判斷及列表腳本操作
下面小編就為大家?guī)硪黄獪\談Python數(shù)據(jù)類型判斷及列表腳本操作。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11在Python中調(diào)用Ping命令,批量IP的方法
今天小編就為大家分享一篇在Python中調(diào)用Ping命令,批量IP的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01使用Python對mongo數(shù)據(jù)庫中字符串型正負數(shù)值比較大小
這篇文章主要介紹了使用Python對mongo數(shù)據(jù)庫中字符串型正負數(shù)值比較大小,2023-04-04Python圖像處理實現(xiàn)兩幅圖像合成一幅圖像的方法【測試可用】
這篇文章主要介紹了Python圖像處理實現(xiàn)兩幅圖像合成一幅圖像的方法,結(jié)合實例形式分析了Python使用Image.blend()接口與Image.composite()接口進行圖像合成的相關(guān)操作技巧,需要的朋友可以參考下2019-01-01