python执行command, 并获取command的执行结果

import subprocess
 
# 方法1:使用subprocess模块的run函数
def run_cmd_1(command):
    """
    运行CMD命令并返回输出结果
    
    参数:
    command (str): 要执行的CMD命令
    
    返回:
    output (str): 命令执行的输出结果
    """
    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): 要执行的CMD命令
    
    返回:
    output (str): 命令执行的输出结果
    """
    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): 要执行的CMD命令
    
    返回:
    output (str): 命令执行的输出结果
    """
    try:
        result = os.system(command)
        output = ""
        if result == 0:
            output = "命令执行成功"
        else:
            output = "命令执行失败"
    except Exception as e:
        output = str(e)
        
    return output
 
# 方法4:使用os模块的popen函数
def run_cmd_4(command):
    """
    运行CMD命令并返回输出结果
    
    参数:
    command (str): 要执行的CMD命令
    
    返回:
    output (str): 命令执行的输出结果
    """
    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): 要执行的CMD命令
    
    返回:
    output (str): 命令执行的输出结果
    """
    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): 要执行的CMD命令
    
    返回:
    output (str): 命令执行的输出结果
    """
    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): 要执行的CMD命令
    
    返回:
    output (str): 命令执行的输出结果
    """
    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): 要执行的CMD命令
    
    返回:
    output (str): 命令执行的输出结果
    """
    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): 要执行的CMD命令
    
    返回:
    output (str): 命令执行的输出结果
    """
    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): 要执行的CMD命令
    
    返回:
    output (str): 命令执行的输出结果
    """
    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)

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值