在Python中,你可以使用os
模块和subprocess
模块来打开命令行
方法1:使用os.system()
函数
import os
os.system('start cmd.exe') # 对于Windows系统
os.system('open /a Terminal') # 对于macOS系统
方法2:使用subprocess
模块
import subprocess
subprocess.run(['start', 'cmd.exe'], check=True) # 对于Windows系统
subprocess.run(['open', '-a', 'Terminal'], check=True) # 对于macOS系统
这些代码片段将打开命令行窗口。请注意,这些示例适用于Windows系统。对于Linux系统,你需要将cmd.exe
替换为bash
或terminal
。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:python命令行怎么打开