如何在Python項(xiàng)目中執(zhí)行cmd命令?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

方法一:os.system
os.system(執(zhí)行的命令) # 源碼 def system(*args, **kwargs): # real signature unknown """ Execute the command in a subshell. """ pass
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)system只把能輸入的內(nèi)容給返回回來(lái)了,其中代碼 0 表示執(zhí)行成功。但是我們沒(méi)有辦法獲取輸出的信息內(nèi)容
popen可以獲取輸出的信息內(nèi)容,它是一個(gè)對(duì)象,可以通過(guò) .read() 去讀取
看完上述內(nèi)容,你們掌握如何在Python項(xiàng)目中執(zhí)行cmd命令的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
標(biāo)題名稱(chēng):如何在Python項(xiàng)目中執(zhí)行cmd命令-創(chuàng)新互聯(lián)
本文路徑:http://www.js-pz168.com/article14/gsede.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶(hù)體驗(yàn)、外貿(mào)網(wǎng)站建設(shè)、搜索引擎優(yōu)化、面包屑導(dǎo)航、建站公司、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容