欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

import subprocess

# 方法1:使用subprocess模塊的run函數(shù)
def run_cmd_1(command):
    """
    運行CMD命令并返回輸出結(jié)果
    
    參數(shù):
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結(jié)果
    """
    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函數(shù)
def run_cmd_2(command):
    """
    運行CMD命令并返回輸出結(jié)果
    
    參數(shù):
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結(jié)果
    """
    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函數(shù)
def run_cmd_3(command):
    """
    運行CMD命令并返回輸出結(jié)果
    
    參數(shù):
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結(jié)果
    """
    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函數(shù)
def run_cmd_4(command):
    """
    運行CMD命令并返回輸出結(jié)果
    
    參數(shù):
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結(jié)果
    """
    try:
        output = os.popen(command).read()
        output = output.strip()
    except Exception as e:
        output = str(e)
        
    return output

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

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

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

# 方法8:使用os模塊的spawn函數(shù)
def run_cmd_8(command):
    """
    運行CMD命令并返回輸出結(jié)果
    
    參數(shù):
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結(jié)果
    """
    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函數(shù)
def run_cmd_9(command):
    """
    運行CMD命令并返回輸出結(jié)果
    
    參數(shù):
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結(jié)果
    """
    try:
        win32api.ShellExecute(0, 'open', 'cmd.exe', '/c {}'.format(command), '', 0)
        output = "命令已啟動"
    except Exception as e:
        output = str(e)
        
    return output

# 方法10:使用win32api模塊的CreateProcess函數(shù)
def run_cmd_10(command):
    """
    運行CMD命令并返回輸出結(jié)果
    
    參數(shù):
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (str): 命令執(zhí)行的輸出結(jié)果
    """
    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命令獲取網(wǎng)絡(luò)配置信息
output = run_cmd_1('ipconfig')
print(output)

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

import subprocess
import os
import commands
import win32api
import win32process

# 方法1:使用subprocess模塊的run函數(shù),返回CompletedProcess對象
def run_cmd_1(command):
    """
    運行CMD命令并返回CompletedProcess對象
    
    參數(shù):
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    result (CompletedProcess): 命令執(zhí)行的結(jié)果對象
    """
    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函數(shù),返回命令的輸出結(jié)果
def run_cmd_2(command):
    """
    運行CMD命令并返回輸出結(jié)果
    
    參數(shù):
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    output (bytes): 命令執(zhí)行的輸出結(jié)果
    """
    try:
        output = subprocess.check_output(command, shell=True)
    except Exception as e:
        output = str(e)
        
    return output

# 方法3:使用subprocess模塊的call函數(shù),返回命令的返回碼
def run_cmd_3(command):
    """
    運行CMD命令并返回返回碼
    
    參數(shù):
    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函數(shù),返回Popen對象
def run_cmd_4(command):
    """
    運行CMD命令并返回Popen對象
    
    參數(shù):
    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函數(shù),返回命令的返回碼
def run_cmd_5(command):
    """
    運行CMD命令并返回返回碼
    
    參數(shù):
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    returncode (int): 命令的返回碼
    """
    try:
        returncode = os.system(command)
    except Exception as e:
        returncode = str(e)
        
    return returncode

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

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

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

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

# 方法10:使用os模塊的spawn函數(shù),返回命令的返回碼
def run_cmd_10(command):
    """
    運行CMD命令并返回返回碼
    
    參數(shù):
    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函數(shù),返回None
def run_cmd_11(command):
    """
    運行CMD命令
    
    參數(shù):
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    None
    """
    try:
        win32api.ShellExecute(0, 'open', 'cmd.exe', '/c {}'.format(command), '', 0)
    except Exception as e:
        pass

# 方法12:使用win32api模塊的CreateProcess函數(shù),返回命令的返回碼和輸出結(jié)果
def run_cmd_12(command):
    """
    運行CMD命令并返回返回碼和輸出結(jié)果
    
    參數(shù):
    command (str): 要執(zhí)行的CMD命令
    
    返回:
    returncode (int): 命令的返回碼
    output (str): 命令執(zhí)行的輸出結(jié)果
    """
    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命令獲取網(wǎng)絡(luò)配置信息,并獲取返回值
result = run_cmd_1('ipconfig')
print(result.returncode)
print(result.stdout)

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

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

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

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

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

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

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

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

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

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

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

總結(jié) 

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

相關(guān)文章

  • Python處理菜單消息操作示例【基于win32ui模塊】

    Python處理菜單消息操作示例【基于win32ui模塊】

    這篇文章主要介紹了Python處理菜單消息操作,結(jié)合實例形式分析了Python基于win32ui模塊實現(xiàn)菜單的創(chuàng)建及菜單項響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下
    2018-05-05
  • 寶塔部署django項目的實現(xiàn)步驟(圖文教程)

    寶塔部署django項目的實現(xiàn)步驟(圖文教程)

    本文主要介紹了寶塔部署django項目的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • 2行Python代碼實現(xiàn)給pdf文件添加水印

    2行Python代碼實現(xiàn)給pdf文件添加水印

    你們在給PDF文件添加水印時,還在手動一頁頁添加嗎?本文小編為大家?guī)砹艘粋€更方便的方法,即用Python的2行代碼來實現(xiàn),感興趣的小伙伴可以學習一下
    2022-02-02
  • Matlab如何實現(xiàn)矩陣復制擴充

    Matlab如何實現(xiàn)矩陣復制擴充

    這篇文章主要介紹了使用Matlab實現(xiàn)矩陣復制擴充的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Python drop()刪除行列的操作方法

    Python drop()刪除行列的操作方法

    這篇文章主要介紹了Python drop()刪除行列的操作方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • Python去除字符串中某個字符的多種實現(xiàn)方法

    Python去除字符串中某個字符的多種實現(xiàn)方法

    這篇文章主要介紹了Python去除字符串中某個字符的多種實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • python 多線程串行和并行的實例

    python 多線程串行和并行的實例

    今天小編就為大家分享一篇python 多線程串行和并行的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • python讀取中文txt文本的方法

    python讀取中文txt文本的方法

    下面小編就為大家分享一篇python讀取中文txt文本的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • python自動保存百度盤資源到百度盤中的實例代碼

    python自動保存百度盤資源到百度盤中的實例代碼

    這篇文章主要介紹了python自動保存百度盤資源到百度盤中的實例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • 跟老齊學Python之模塊的加載

    跟老齊學Python之模塊的加載

    這篇文章主要介紹了跟老齊學Python之模塊的加載,需要的朋友可以參考下
    2014-10-10

最新評論