python读取系统信息
python读取系统信息的一些方法,在此记录一下,方便尔后查询。

platform模块

root@cubieboard:~# python
Python 2.7.3 (default, Mar 14 2014, 17:55:54) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.version()
'#2 PREEMPT Sat Nov 15 14:20:48 CST 2014'
>>> platform.platform()
'Linux-3.4.79-armv7l-with-debian-7.6'
>>> platform.system()
'Linux'
>>> platform.machine()
'armv7l'
>>> platform.python_build()
('default', 'Mar 14 2014 17:55:54')
>>> platform.python_version()
'2.7.3'
>>> platform.uname()
('Linux', 'cubieboard', '3.4.79', '#2 PREEMPT Sat Nov 15 14:20:48 CST 2014', 'armv7l', '')
>>> platform.architecture()
('32bit', '')
在windows上,专门还有个platform.win32_ver() 可用

获取用户名

>>> import getpass
>>> getpass.getuser()
'root'   

获取环境变量

>>> import os
>>> import pwd
>>> os.environ['LANG']
'en_US.UTF-8'
>>> print os.getenv('LANG')
en_US.UTF-8
>>> print os.getenv('PWD')
/root
>>> print os.getenv('HOME')
/root
>>> print os.getenv('USER')
root
>>> print os.getenv('SHELL')
/bin/bash
>>> pwd.getpwuid(os.getuid())
pwd.struct_passwd(pw_name='root', pw_passwd='x', pw_uid=0, pw_gid=0, pw_gecos='root', pw_dir='/root', pw_shell='/bin/bash')
>>> pwd.getpwuid(os.getuid())[0] #获得用户名
'root'
>>> pwd.getpwuid(os.getuid())[5]  #获得家目录
'/root'
>>> pwd.getpwuid(os.getuid())[6]   #获得shell
'/bin/bash'
还有个os.environ.get,会返回所有环境变量为一个字典