python10种方式运行cmd命令的高级技巧

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_USESHOWWINDOWoutput = 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 subprocessimport osimport commandsimport win32apiimport win32process# 方法1:使用subprocess模块的run函数,返回CompletedProcess对象def run_cmd_1(command):"""运行CMD命令并返回CompletedProcess对象参数:command (str): 要执行的CMD命令返回:result (CompletedProcess): 命令执行的结果对象"""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): 要执行的CMD命令返回:output (bytes): 命令执行的输出结果"""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): 要执行的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): 要执行的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): 要执行的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): 要执行的CMD命令返回:output (str): 命令执行的输出结果"""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): 要执行的CMD命令返回:output (str): 命令执行的输出结果"""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): 要执行的CMD命令返回:status (int): 命令的返回码output (str): 命令执行的输出结果"""try:status, output = commands.getstatusoutput(command)except Exception as e:status = str(e)output = ""return status, output# 方法9:使用os模块的startfile函数,返回Nonedef run_cmd_9(command):"""运行CMD命令参数:command (str): 要执行的CMD命令返回:None"""try:os.startfile(command)except Exception as e:pass# 方法10:使用os模块的spawn函数,返回命令的返回码def run_cmd_10(command):"""运行CMD命令并返回返回码参数:command (str): 要执行的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函数,返回Nonedef run_cmd_11(command):"""运行CMD命令参数:command (str): 要执行的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): 要执行的CMD命令返回:returncode (int): 命令的返回码output (str): 命令执行的输出结果"""try:si = win32process.STARTUPINFO()si.dwFlags |= win32process.STARTF_USESHOWWINDOWoutput = win32api.CreateProcess(None, command, None, None, False, 0, None, None, si)[1].read()returncode = 0except 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)