最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python運行cmd命令10種方式并獲得返回值的高級技巧

 更新時間:2024年03月18日 11:14:40   作者:《代碼愛好者》  
這篇文章主要給大家介紹了關于python運行cmd命令10種方式并獲得返回值的高級技巧,主要包括python腳本執(zhí)行CMD命令并返回結果的例子使用實例、應用技巧,文中通過代碼介紹的非常詳細,需要的朋友可以參考下

python10種方式運行cmd命令的高級技巧

import subprocess

# 方法1:使用subprocess模塊的run函數
def run_cmd_1(command):
    """
    運行CMD命令并返回輸出結果
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結果
    """
    try:
        result = subprocess.run(command, shell=True, capture_output=True, text=True)
        if result.returncode == 0:
            output = result.stdout.strip()
        else:
            output = result.stderr.strip()
    except Exception as e:
        output = str(e)
        
    return output

# 方法2:使用subprocess模塊的check_output函數
def run_cmd_2(command):
    """
    運行CMD命令并返回輸出結果
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結果
    """
    try:
        output = subprocess.check_output(command, shell=True, text=True)
        output = output.strip()
    except Exception as e:
        output = str(e)
        
    return output

# 方法3:使用os模塊的system函數
def run_cmd_3(command):
    """
    運行CMD命令并返回輸出結果
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結果
    """
    try:
        result = os.system(command)
        output = ""
        if result == 0:
            output = "命令執(zhí)行成功"
        else:
            output = "命令執(zhí)行失敗"
    except Exception as e:
        output = str(e)
        
    return output

# 方法4:使用os模塊的popen函數
def run_cmd_4(command):
    """
    運行CMD命令并返回輸出結果
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結果
    """
    try:
        output = os.popen(command).read()
        output = output.strip()
    except Exception as e:
        output = str(e)
        
    return output

# 方法5:使用commands模塊的getoutput函數
def run_cmd_5(command):
    """
    運行CMD命令并返回輸出結果
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結果
    """
    try:
        output = commands.getoutput(command)
        output = output.strip()
    except Exception as e:
        output = str(e)
        
    return output

# 方法6:使用commands模塊的getstatusoutput函數
def run_cmd_6(command):
    """
    運行CMD命令并返回輸出結果
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結果
    """
    try:
        status, output = commands.getstatusoutput(command)
        output = output.strip()
    except Exception as e:
        output = str(e)
        
    return output

# 方法7:使用os模塊的startfile函數
def run_cmd_7(command):
    """
    運行CMD命令并返回輸出結果
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結果
    """
    try:
        os.startfile(command)
        output = "命令已啟動"
    except Exception as e:
        output = str(e)
        
    return output

# 方法8:使用os模塊的spawn函數
def run_cmd_8(command):
    """
    運行CMD命令并返回輸出結果
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結果
    """
    try:
        output = os.spawnl(os.P_WAIT, 'cmd.exe', '/c', command)
        output = output.strip()
    except Exception as e:
        output = str(e)
        
    return output

# 方法9:使用win32api模塊的ShellExecute函數
def run_cmd_9(command):
    """
    運行CMD命令并返回輸出結果
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結果
    """
    try:
        win32api.ShellExecute(0, 'open', 'cmd.exe', '/c {}'.format(command), '', 0)
        output = "命令已啟動"
    except Exception as e:
        output = str(e)
        
    return output

# 方法10:使用win32api模塊的CreateProcess函數
def run_cmd_10(command):
    """
    運行CMD命令并返回輸出結果
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結果
    """
    try:
        si = win32process.STARTUPINFO()
        si.dwFlags |= win32process.STARTF_USESHOWWINDOW
        output = win32api.CreateProcess(None, command, None, None, False, 0, None, None, si)[1].read()
        output = output.strip()
    except Exception as e:
        output = str(e)
        
    return output

# 示例:運行ipconfig命令獲取網絡配置信息
output = run_cmd_1('ipconfig')
print(output)

python12種方式運行cmd命令并獲得返回值的高級技巧

import subprocess
import os
import commands
import win32api
import win32process

# 方法1:使用subprocess模塊的run函數,返回CompletedProcess對象
def run_cmd_1(command):
    """
    運行CMD命令并返回CompletedProcess對象
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    result (CompletedProcess): 命令執(zhí)行的結果對象
    """
    try:
        result = subprocess.run(command, shell=True, capture_output=True, text=True)
    except Exception as e:
        result = str(e)
        
    return result

# 方法2:使用subprocess模塊的check_output函數,返回命令的輸出結果
def run_cmd_2(command):
    """
    運行CMD命令并返回輸出結果
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (bytes): 命令執(zhí)行的輸出結果
    """
    try:
        output = subprocess.check_output(command, shell=True)
    except Exception as e:
        output = str(e)
        
    return output

# 方法3:使用subprocess模塊的call函數,返回命令的返回碼
def run_cmd_3(command):
    """
    運行CMD命令并返回返回碼
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    returncode (int): 命令的返回碼
    """
    try:
        returncode = subprocess.call(command, shell=True)
    except Exception as e:
        returncode = str(e)
        
    return returncode

# 方法4:使用subprocess模塊的Popen函數,返回Popen對象
def run_cmd_4(command):
    """
    運行CMD命令并返回Popen對象
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    process (Popen): Popen對象
    """
    try:
        process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    except Exception as e:
        process = str(e)
        
    return process

# 方法5:使用os模塊的system函數,返回命令的返回碼
def run_cmd_5(command):
    """
    運行CMD命令并返回返回碼
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    returncode (int): 命令的返回碼
    """
    try:
        returncode = os.system(command)
    except Exception as e:
        returncode = str(e)
        
    return returncode

# 方法6:使用os模塊的popen函數,返回命令的輸出結果
def run_cmd_6(command):
    """
    運行CMD命令并返回輸出結果
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結果
    """
    try:
        output = os.popen(command).read()
    except Exception as e:
        output = str(e)
        
    return output

# 方法7:使用commands模塊的getoutput函數,返回命令的輸出結果
def run_cmd_7(command):
    """
    運行CMD命令并返回輸出結果
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結果
    """
    try:
        output = commands.getoutput(command)
    except Exception as e:
        output = str(e)
        
    return output

# 方法8:使用commands模塊的getstatusoutput函數,返回命令的返回碼和輸出結果
def run_cmd_8(command):
    """
    運行CMD命令并返回返回碼和輸出結果
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    status (int): 命令的返回碼
    output (str): 命令執(zhí)行的輸出結果
    """
    try:
        status, output = commands.getstatusoutput(command)
    except Exception as e:
        status = str(e)
        output = ""
        
    return status, output

# 方法9:使用os模塊的startfile函數,返回None
def run_cmd_9(command):
    """
    運行CMD命令
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    None
    """
    try:
        os.startfile(command)
    except Exception as e:
        pass

# 方法10:使用os模塊的spawn函數,返回命令的返回碼
def run_cmd_10(command):
    """
    運行CMD命令并返回返回碼
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    returncode (int): 命令的返回碼
    """
    try:
        returncode = os.spawnl(os.P_WAIT, 'cmd.exe', '/c', command)
    except Exception as e:
        returncode = str(e)
        
    return returncode

# 方法11:使用win32api模塊的ShellExecute函數,返回None
def run_cmd_11(command):
    """
    運行CMD命令
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    None
    """
    try:
        win32api.ShellExecute(0, 'open', 'cmd.exe', '/c {}'.format(command), '', 0)
    except Exception as e:
        pass

# 方法12:使用win32api模塊的CreateProcess函數,返回命令的返回碼和輸出結果
def run_cmd_12(command):
    """
    運行CMD命令并返回返回碼和輸出結果
    
    參數:
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    returncode (int): 命令的返回碼
    output (str): 命令執(zhí)行的輸出結果
    """
    try:
        si = win32process.STARTUPINFO()
        si.dwFlags |= win32process.STARTF_USESHOWWINDOW
        output = win32api.CreateProcess(None, command, None, None, False, 0, None, None, si)[1].read()
        returncode = 0
    except Exception as e:
        returncode = str(e)
        output = ""
        
    return returncode, output

# 示例:運行ipconfig命令獲取網絡配置信息,并獲取返回值
result = run_cmd_1('ipconfig')
print(result.returncode)
print(result.stdout)

# 示例:運行dir命令獲取文件列表,并獲取輸出結果
output = run_cmd_2('dir')
print(output)

# 示例:運行ping命令測試網絡連接,并獲取返回碼
returncode = run_cmd_3('ping google.com')
print(returncode)

# 示例:運行tasklist命令獲取進程列表,并獲取Popen對象
process = run_cmd_4('tasklist')
print(process.stdout.readline())

# 示例:運行ipconfig命令獲取網絡配置信息,并獲取返回碼
returncode = run_cmd_5('ipconfig')
print(returncode)

# 示例:運行dir命令獲取文件列表,并獲取輸出結果
output = run_cmd_6('dir')
print(output)

# 示例:運行ping命令測試網絡連接,并獲取輸出結果
output = run_cmd_7('ping google.com')
print(output)

# 示例:運行tasklist命令獲取進程列表,并獲取返回碼和輸出結果
status, output = run_cmd_8('tasklist')
print(status)
print(output)

# 示例:運行notepad命令打開記事本應用程序
run_cmd_9('notepad')

# 示例:運行ping命令測試網絡連接,并獲取返回碼
returncode = run_cmd_10('ping google.com')
print(returncode)

# 示例:運行notepad命令打開記事本應用程序
run_cmd_11('notepad')

# 示例:運行tasklist命令獲取進程列表,并獲取返回碼和輸出結果
returncode, output = run_cmd_12('tasklist')
print(returncode)
print(output)

總結 

到此這篇關于python運行cmd命令10種方式并獲得返回值的文章就介紹到這了,更多相關python運行cmd命令獲得返回值內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python3 SSH遠程連接服務器的方法示例

    Python3 SSH遠程連接服務器的方法示例

    這篇文章主要介紹了Python3 SSH遠程連接服務器的方法示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • 詳解Python中的內置常量的使用

    詳解Python中的內置常量的使用

    Python作為一種功能強大的編程語言,提供了豐富的內置常量來簡化編程過程,本文將深入探討Python中的內置常量,并提供豐富的示例代碼來演示其用法,希望對大家有所幫助
    2024-03-03
  • Python獲取網段內ping通IP的方法

    Python獲取網段內ping通IP的方法

    今天小編就為大家分享一篇Python獲取網段內ping通IP的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • PyTorch實現更新部分網絡,其他不更新

    PyTorch實現更新部分網絡,其他不更新

    今天小編就為大家分享一篇PyTorch實現更新部分網絡,其他不更新,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python中bs4的soup.find()和soup.find_all()用法示例小結

    Python中bs4的soup.find()和soup.find_all()用法示例小結

    我們在使用python對網頁爬蟲的時候,經常會得到一些html數據,因此我們就會利用soup.find()和soup.find_all()方法來篩選出想要的數據,這篇文章主要介紹了Python中bs4的soup.find()和soup.find_all()用法,需要的朋友可以參考下
    2024-12-12
  • Python腳本輕松實現檢測麥克風功能

    Python腳本輕松實現檢測麥克風功能

    在進行音頻處理或開發(fā)需要使用麥克風的應用程序時,確保麥克風功能正常是非常重要的,本文將介紹一個簡單的Python腳本,能夠幫助我們檢測本地麥克風的功能,需要的可以參考下
    2025-08-08
  • Python中的sort方法、sorted函數與lambda表達式及用法詳解

    Python中的sort方法、sorted函數與lambda表達式及用法詳解

    文章對比了Python中l(wèi)ist.sort()與sorted()函數的區(qū)別,指出sort()原地排序返回None,sorted()生成新列表,本文結合實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2025-09-09
  • 使用Python 操作 xmind 繪制思維導圖的詳細方法

    使用Python 操作 xmind 繪制思維導圖的詳細方法

    在平時的工作中當我們要總結一些知識的時候就需要一款工具來畫畫流程圖,這里推薦 XMind 軟件,用 Xmind 繪制的思維導圖看起來思路清晰,那么今天的文章介紹關于思維導圖的相關知識以及用 Python 如何操作 Xmind 繪制思維導圖
    2021-10-10
  • Ranorex通過Python將報告發(fā)送到郵箱的方法

    Ranorex通過Python將報告發(fā)送到郵箱的方法

    這篇文章主要介紹了Ranorex通過Python將報告發(fā)送到郵箱的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01
  • 教你怎么用python實現字符串轉日期

    教你怎么用python實現字符串轉日期

    今天教各位小伙伴怎么用python實現字符串轉日期,文中有非常詳細的代碼示例,對正在學習python的小伙伴很有幫助,需要的朋友可以參考下
    2021-05-05

最新評論

台安县| 泰来县| 三门县| 安图县| 昌江| 深水埗区| 陇南市| 博乐市| 鹤峰县| 富宁县| 抚州市| 石家庄市| 中江县| 隆德县| 团风县| 秦皇岛市| 鹰潭市| 天等县| 百色市| 新宾| 呼图壁县| 保德县| 壶关县| 巴楚县| 金塔县| 昌平区| 高邑县| 肇东市| 沁水县| 屏东县| 玉溪市| 伽师县| 绥德县| 宜章县| 沁水县| 大英县| 太康县| 扬中市| 迁安市| 江西省| 普兰店市|