python - 如何检测 Python 是否在 Git Bash 终端或 Windows cmd 中运行?
问题描述
我想让我的 Python 脚本能够检测它是在 Git Bash 终端还是在 Windows cmd 命令行界面中执行的。例如,我正在尝试编写一个函数来清除终端(无论它是哪个终端),例如,clear
如果在 Git Bash 中或cls
在 cmd 中,则回显命令。
我尝试使用 sys.platform 来检测这一点,但win32
无论它运行在哪种类型的终端中,它都会返回。
解决方案
请尝试使用os
和psutil
模块。
例如,
import os, psutil # Get the parent process name.
pprocName = psutil.Process(os.getppid()).name()
然后你可以有你的逻辑取决于外壳。
此外,您可能需要检查https://www.geeksforgeeks.org/clear-screen-python/
推荐阅读
- php - PHP HTML 简单 DOM 提取属性
- c++ - 有没有办法在 Visual Studio 2019 中为当前的 .cpp 文件加载 .h 文件?
- error-handling - 如何初始化“Word”的这个实例
- spring - 它没有 HTTP ok 状态-haproxy 后面的 Spring boot keycloak 版本 4.0.0 final
- c - 为什么没有 GCC 警告在 C 中重新定义结构?
- django - Django Cassandra Engine 连接到多个数据库配置
- mongodb - 如何使用nestJS将文件上传到mongoose上的mongodb?
- json - 如何在颤振中使用 l10n 本地化翻译从 JSON 文件填充的列表?
- r - 是什么导致 h2o.grid 函数出现此错误?
- sql - 引用表“Doctors”中没有与 FK 匹配的主键或候选键