Python:获取Windows操作系统版本和体系结构

Tho*_*fin 6 python windows cpu-architecture

首先,我不认为这个问题
与Python中检测64位操作系统(Windows)重复,
因为它还没有得到彻底的回答.

唯一接近的答案是:

使用sys.getwindowsversion()或存在PROGRAMFILES(X86)(if 'PROGRAMFILES(X86)' in os.environ)

但:

  • Windows环境变量PROGRAMFILES(X86)可靠吗?我担心任何人都可以创建它,即使系统中没有它.
  • 如何使用sys.getwindowsversion()才能获得架构?

关于sys.getwindowsversion():
链接http://docs.python.org/library/sys.html#sys.getwindowsversion
将我们引导至http://msdn.microsoft.com/en-us/library/ms724451%28VS.85%29. aspx
但我没有看到任何与架构相关的东西(32位/ 64位).
此外,返回元组中的平台元素似乎独立于体系结构.

最后一点:我正在寻找一个使用python 2.5和从Windows XP开始的Windows版本的解决方案

谢谢!

编辑:
相关信息可以在
http://msdn.microsoft.com/en-us/library/ms724340%28v=VS.85%29.aspx
获得,但我怎么能用python得到这个?

Edit2:在64位窗口上,带有32位python解释器:

  • os.environ ["PROCESSOR_ARCHITECTURE"]返回
    • "86"
  • platform.architecture()返回
    • ('32位','WindowsPE')

Cur*_*ice 18

我认为平台模块确实是获取此信息的最佳方式.

  >>> import platform
  >>> platform.platform()
  'Windows-7-6.1.7601-SP1'
  platform.processor()
  'Intel64 Family 6 Model 42 Stepping 7, GenuineIntel'
Run Code Online (Sandbox Code Playgroud)

我没有看到从这里得到32/64位窗口的确切答案,所以我建议:

  try:
      os.environ["PROGRAMFILES(X86)"]
      bits = 64
  except:
      bits = 32
  print "Win{0}".format(bits)
Run Code Online (Sandbox Code Playgroud)

或者,如果你需要知道你正在运行哪种Python(因为你可以在x64 Windows下运行x32 python):

x32 python x64 windows:
>>> platform.architecture()
('32bit', 'WindowsPE')
>>> sys.version
'2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]'

x64 python x64 windows:
>>> platform.architecture()
('64bit', 'WindowsPE')
>>> sys.version
'2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)]'
Run Code Online (Sandbox Code Playgroud)


Luk*_*uke 6

这些变量显示Windows上的当前运行时状态:


@rem Test environment using this table:
@rem
@rem Environment Variable       32bit Native    64bit Native    WOW64
@rem PROCESSOR_ARCHITECTURE     x86             AMD64           x86
@rem PROCESSOR_ARCHITEW6432     undefined       undefined       AMD64
@rem