python - 如何检查我是否在 Python 中的 Windows 上运行?

我找到了平台模块,但它说它返回“Windows”,并且在我的机器上返回“Microsoft”。我在 stackoverflow 上的另一个线程中注意到它有时会返回“Vista”。

那么,问题来了,如何实现?

if is_windows():
  ...

以向前兼容的方式?如果我必须检查诸如“Vista”之类的东西,那么它会在下一个版本的 Windows 出现时中断。


注意:声称这是重复问题的答案实际上并未回答问题 is_windows。他们回答了“什么平台”的问题。由于存在多种类型的窗口,它们都没有全面描述如何获得 isWindows 的答案。

最佳答案

Python os模块

专门针对 Python 3.6/3.7:

os.name: The name of the operating system dependent module imported. The following names have currently been registered: 'posix', 'nt', 'java'.

在你的情况下,你想检查 'nt' 作为 os.name 输出:

import os

if os.name == 'nt':
     ...

os.name上也有注释:

See also sys.platform has a finer granularity. os.uname() gives system-dependent version information.

The platform module provides detailed checks for the system’s identity.

https://stackoverflow.com/questions/1325581/

相关文章:

python - 从 pandas.DataFrame 中选择复杂的标准

linux - 如何在 grep 中抑制二进制文件匹配结果

linux - 如何就地对文件进行排序?

python - 在 Python 中以编程方式生成视频或动画 GIF?

python - `ValueError: cannot reindex from a duplic

linux - 有没有办法按列 'uniq'?

python - 使用 Python 的 os.path,我如何上一个目录?

linux - 如何在linux中设置环境变量LD_LIBRARY_PATH

linux - 我可以用/etc/hosts 映射一个主机名*和*一个端口吗?

python - Python 的 any 和 all 函数是如何工作的?