在开发工作中,经常需要获取系统的的各种信息,使用python会很方便帮助获得。
使用sys模块
sys模块在标准库中,他提供很一些基本的系统信息函数。
>>>import sys
>>>sys.platform
'win32'
>>>sys.getdefaultencoding()
'ascii'
>>>sys.getfilesystemencoding()
'mbcs'
>>> sys.getwindowsversion()
sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1')
>>>sys.maxsize
2147483647
在这里sys.maxsize得到的是系统支持的最大的正整数,通过表达式is_64bits = sys.maxsize > 2**32,可以得到系统是否为64位。
使用platform模块
如果想得到更多细节的东西,就要使用platform模块了,该模块提供了很多获得系统信息的函数。
>>>import platform
>>>platform.uname()
('Windows', 'E7B499BAE703F3', '7', '6.1.7601', 'AMD64', 'Intel64 Family 6 Model
37 Stepping 5, GenuineIntel')
>>>platform.system()
'Windows'
>>>platform.node()
'E7B499BAE703F3'
>>>platform.release()
'7'
>>>platform.version()
'6.1.7601'
>>>platform.machine()
'AMD64'
>>>platform.processor()
'Intel64 Family 6 Model 37 Stepping 5, GenuineIntel'
platform.uname()得到的是一个tuple,分别代表下面的各个属性。
platform.architeture()函数如果不传入任何参数,默认返回的是包含架构位数以及Python可执行的格式的一个tuple,如下:
>>>platform.architeture()
('32bit', 'WindowsPE')
platform._supported_dists返回的是platform模块支持的系统列表
>>>platform._supported_dists
('SuSE', 'debian', 'fedora', 'redhat', 'centos', 'mandrake', 'mandriva', 'rocks'
, 'slackware', 'yellowdog', 'gentoo', 'UnitedLinux', 'turbolinux')
platform还提供了一些属性可以得到平台安装的Python的信息
>>>platform.python_implementation()
'CPython'
>>> platform.python_build()
('r271:86832', 'Nov 27 2010 18:30:46')
>>> platform.python_compiler()
'MSC v.1500 32 bit (Intel)'
>>> platform.python_branch()
'tags/r271'
>>> platform.python_version()
'2.7.1'
>>> platform.python_revision()
'86832'
>>> platform.python_version_tuple()
('2', '7', '1')
需要介绍一下,platform.python_branch()返回的是Python的实现的SCM的branch,platform.python_revision()返回的是Python的实现的SCM的修订版本。