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