python platform和pwd模

一、platform模組

platform運行在標準庫中,它有很多運行我們獲得眾多系統資訊的函數。

>>> import platform

>>> platform.uname()

('Linux', 'gitlab.test.com', '3.10.0-327.el7.x86_64', '#1 SMP Thu Nov 19 22:10:57 UTC 2015', 'x86_64', 'x86_64')

>>> platform.system()

'Linux'

>>> platform.release()

'3.10.0-327.el7.x86_64'

>>> platform.linux_distribution()

('CentOS Linux', '7.2.1511', 'Core')

額外:

導入modules,import與from…import的不同之處在於,簡單說:

 如果你想在程式中用argv代表sys.argv,

則可使用:from sys import argv

一般說來,應該避免使用from..import而使用import語句,

因為這樣可以使你的程式更加易讀,也可以避免名稱的衝突

二、pwd模組

getpwall函數返回一個包含所有可用用戶資料庫入口的列表,你可以使用它搜索一個用戶。

getpwall    getpwnam  getpwuid    三個常用函數

>>> pwd.getpwuid(0)

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.getpwnam('root')

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')