在Python中,终止命令行窗口(如CMD)的方法有多种,以下是几种常见的方法:
1. 使用`exit()`或`quit()`函数:
```python
exit() 或者 quit()
2. 使用`Ctrl+Z`后回车:
```plaintext
Ctrl+Z
3. 使用`os`模块的`system`函数,通过发送`taskkill`命令来强制关闭窗口:
```python
import os
os.system('taskkill /f /im cmd.exe') 强制关闭所有cmd窗口
4. 使用`subprocess`模块打开一个新的CMD窗口,并发送关闭命令:
```python
import subprocess
cmd_process = subprocess.Popen('cmd.exe', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
cmd_process.stdin.write(b'exit\n') 发送 exit 命令
cmd_process.stdin.flush() 刷新缓冲区以确保命令被执行
请选择适合您需求的方法来终止CMD命令窗口