用Python 執行cmd命令
我們通常可以使用os模塊的命令進行執行cmd
方法一:os.systemos.system(執行的命令)# 源碼def system(*args, **kwargs): # real signature unknown ''' Execute the command in a subshell. ''' pass方法二:os.popen(執行的命令)
os.popen(執行的命令)# 源碼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只把能輸入的內容給返回回來了,其中代碼 0 表示執行成功。但是我們沒有辦法獲取輸出的信息內容 popen可以獲取輸出的信息內容,它是一個對象,可以通過 .read() 去讀取
以上就是用Python 執行cmd命令的詳細內容,更多關于python 執行cmd命令的資料請關注好吧啦網其它相關文章!
相關文章: