如何脚本启动cmd命令参数

原创
ithorizon 6个月前 (10-20) 阅读数 20 #综合信息
通过以下三种方法可在脚本中启动 cmd 命令并传入参数:使用 os.system() 函数使用 subprocess 模块使用 ctypes 模块

通过脚本启动 Cmd 命令参数

在脚本中启动 Cmd 命令并传入参数,可以通过以下方法实现:

方法 1:使用 os.system() 函数

import os

# 要执行的 Cmd 命令
cmd = "cmd /c start notepad.exe"

# 在脚本中启动 Cmd 命令
os.system(cmd)
登录后复制

方法 2:使用 subprocess 模块

import subprocess

# 要执行的 Cmd 命令和参数
cmd = ["cmd", "/c", "start", "notepad.exe"]

# 在脚本中启动 Cmd 命令
subprocess.run(cmd)
登录后复制

方法 3:使用 ctypes 模块

import ctypes

# 要执行的 Cmd 命令
cmd = "start notepad.exe"

# 启动 Cmd 命令
kernel32 = ctypes.windll.kernel32
handle = kernel32.CreateProcessW(None, cmd, None, None, False, 0, None, None, None)
if handle == 0:
    raise WindowsError()
登录后复制

这三种方法都可以实现通过脚本启动 Cmd 命令并传入参数的功能。选择哪种方法取决于具体需求和偏好。

以上就是如何脚本启动cmd命令参数的详细内容,更多请关注IT视界其它相关文章!



热门