亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

用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)文章

最新評論