用Python 執(zhí)行cmd命令
我們通常可以使用os模塊的命令進(jìn)行執(zhí)行cmd
方法一:os.systemos.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)容,它是一個(gè)對象,可以通過 .read() 去讀取
以上就是用Python 執(zhí)行cmd命令的詳細(xì)內(nèi)容,更多關(guān)于python 執(zhí)行cmd命令的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息2. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法3. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明4. jsp網(wǎng)頁實(shí)現(xiàn)貪吃蛇小游戲5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向7. asp中response.write("中文")或者js中文亂碼問題8. PHP設(shè)計(jì)模式中工廠模式深入詳解9. CSS hack用法案例詳解10. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過程(親測可用)
