Python腳本獲取操作系統(tǒng)版本信息
查看系統(tǒng)版本信息是一件家常便飯的事情,有時候需要將版本信息錄入到資產(chǎn)管理系統(tǒng)中,如果每次手動的去查詢這些信息再錄入系統(tǒng)那么是一件令人呢頭疼的事情,如果采用腳本去完成這件事情,那么情況就有所不同了。
在Python的世界里,獲取Windows版本信息和Linux的版本信息都可以采用platform模塊,但platform模塊也不是萬能的,有些特殊的信息(比如Windows的內(nèi)部版本號)這個模塊拿不到,那么只能另辟蹊徑了。
在Linux系統(tǒng)中,可以簡單的認為一切都是文件,那么就算沒有現(xiàn)成的命令可用時,可以用open()文件的方法通過對文件的讀寫控制它。而在Windows的大部分信息在注冊表中都能查到,因此可以從注冊表上下手。Windows注冊表是一個好東西,拿數(shù)據(jù)就像在Linux下一切都是文件一樣方便,如果想用Python訪問注冊表,除了權(quán)限外就是需要模塊了,在Python中_winreg是一個內(nèi)置模塊,通過這一模塊可以對注冊表進行讀寫。
本腳本收集了一些獲取版本信息的常見方法,除了platform模塊外,還有其他的模塊可供使用,因為platform模塊不是內(nèi)置模塊,因此需要額外安裝。Windows下運行腳本需要考慮權(quán)限問題和中文字符的問題,解決Python打印中文字符的問題是通過腳本中的get_system_encoding()函數(shù)實現(xiàn)的,這個函數(shù)取自Django,經(jīng)過測試這個函數(shù)還是非常好用的。
注:在PyCharm中,經(jīng)常遇到Run窗口打印出的中文顯示亂碼,代碼中沒有經(jīng)過正確轉(zhuǎn)碼是一方面,而IDE的編碼設置也是一方面。如果是在Windows下開發(fā),那么建議代碼用UTF-8編寫,IDE的編碼則設置為“GBK”,設置方法“File”-->"Settings"-->"Editor"-->"File Encoding"-->"IDE Encoding"選擇“<System Default (now GBK)>”, "Project Encoding"選擇UTF-8保證代碼的編碼一致性。

腳本如下:
#!/usr/bin/python
# encoding: utf-8
# -*- coding: utf8 -*-
"""
Created by PyCharm.
File: LinuxBashShellScriptForOps:getSystemVersion.py
User: Guodong
Create Date: 2016/12/16
Create Time: 14:51
"""
import sys
import os
import platform
import subprocess
import codecs
import locale
def get_system_encoding():
"""
The encoding of the default system locale but falls back to the given
fallback encoding if the encoding is unsupported by python or could
not be determined. See tickets #10335 and #5846
"""
try:
encoding = locale.getdefaultlocale()[1] or 'ascii'
codecs.lookup(encoding)
except Exception:
encoding = 'ascii'
return encoding
DEFAULT_LOCALE_ENCODING = get_system_encoding()
mswindows = (sys.platform == "win32") # learning from 'subprocess' module
linux = (sys.platform == "linux2")
hidden_hostname = True
if mswindows:
uname = list(platform.uname())
if hidden_hostname:
uname[1] = "hidden_hostname"
print uname
import _winreg
try:
reg_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion")
if reg_key:
ProductName = _winreg.QueryValueEx(reg_key, "ProductName")[0] or None
EditionId = _winreg.QueryValueEx(reg_key, "EditionId")[0] or None
ReleaseId = _winreg.QueryValueEx(reg_key, "ReleaseId")[0] or None
CurrentBuild = _winreg.QueryValueEx(reg_key, "CurrentBuild")[0] or None
BuildLabEx = _winreg.QueryValueEx(reg_key, "BuildLabEx")[0][:9] or None
print (ProductName, EditionId, ReleaseId, CurrentBuild, BuildLabEx)
except Exception as e:
print e.message.decode(DEFAULT_LOCALE_ENCODING)
if linux:
uname = list(platform.uname())
if hidden_hostname:
uname[1] = "hidden_hostname"
print uname
proc_obj = subprocess.Popen(r'uname -a', shell=True, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
result = proc_obj.stdout.read().strip().decode(DEFAULT_LOCALE_ENCODING)
if result:
print result
if os.path.isfile("/proc/version"):
with open("/proc/version", 'r') as f:
content = f.read().strip()
if content != "":
print content
if os.path.isfile("/etc/issue"):
with open("/etc/issue", 'r') as f:
content = f.read().strip()
if content != "":
print content
截圖如下:
(1)注冊表信息獲取位置:

(2)Windows環(huán)境下的輸出:

(3)Linux環(huán)境下的輸出:

相關(guān)文章
Python數(shù)據(jù)分析中常見統(tǒng)計方法詳解
數(shù)據(jù)分析是現(xiàn)代社會中不可或缺的一部分,通過對數(shù)據(jù)的統(tǒng)計和分析,我們可以得出有用的信息和見解,本文將介紹在?Python?中常見的數(shù)據(jù)統(tǒng)計方法,希望對大家有所幫助2024-02-02
Python字典創(chuàng)建 遍歷 添加等實用基礎操作技巧
字段是Python是字典中唯一的鍵-值類型,本文講述了Python中字典如何創(chuàng)建 遍歷 添加等實用基礎操作技巧,內(nèi)容非?;A但非常重要,一定要熟練掌握2018-09-09
關(guān)于Pycharm配置翻譯插件Translation報錯更新TTK失敗不能使用的問題
這篇文章主要介紹了關(guān)于Pycharm配置翻譯插件Translation報錯更新TTK失敗不能使用的問題,本文通過圖文并茂的形式給大家分享解決方案,需要的朋友可以參考下2022-04-04
深入理解Python中的 __new__ 和 __init__及區(qū)別介紹
這篇文章主要介紹了深入理解Python中的 __new__ 和 __init__及區(qū)別介紹,這兩個方法的主要區(qū)別在于:__new__ 負責對象的創(chuàng)建而 __init__ 負責對象的初始化。具體內(nèi)容詳情大家跟隨小編一起看看吧2018-09-09

