我找到了平台模块,但它说它返回“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/