python讀取注冊(cè)表中值的方法
在Python的標(biāo)準(zhǔn)庫中,_winreg.pyd可以操作Windows的注冊(cè)表,另外第三方的win32庫封裝了大量的Windows API,使用起來也很方便。不過這里介紹的是使用_winreg操作注冊(cè)表,畢竟是Python自帶的標(biāo)準(zhǔn)庫,無需安裝第三方庫。
下面的例子是通過Python獲取Windows XP下已經(jīng)安裝的補(bǔ)丁號(hào)。Windows的補(bǔ)丁號(hào)都在“HKEY_LOCAL_MACHINE\SOFTWARE\\Microsoft\\Updates”下,通過循環(huán)下面所有的目錄節(jié)點(diǎn),如果找到的名稱符合正則表達(dá)式KB(\d{6}).*,則表示是一個(gè)補(bǔ)丁號(hào)。
從例子可以看出操作起來非常的簡單和快速。
# -*- coding: utf-8 -*-
# 獲取Windows的已打的補(bǔ)丁號(hào)
from _winreg import *
import re
def subRegKey(key, pattern, patchlist):
# 個(gè)數(shù)
count = QueryInfoKey(key)[0]
for index in range(count):
# 獲取標(biāo)題
name = EnumKey(key, index)
result = patch.match(name)
if result:
patchlist.append(result.group(1))
sub = OpenKey(key, name)
subRegKey(sub, pattern, patchlist)
CloseKey(sub)
if __name__ == '__main__':
patchlist = []
updates = 'SOFTWARE\\Microsoft\\Updates'
patch = re.compile('(KB\d{6}).*')
key = OpenKey(HKEY_LOCAL_MACHINE, updates)
subRegKey(key, patch, patchlist)
print 'Count: ' + str(len(patchlist))
for p in patchlist:
print p
CloseKey(key)
下面內(nèi)容轉(zhuǎn)自 Python Standard Library12.13 The _winreg Module
(Windows only, New in 2.0) The _winreg module provides a basic interface to the Windows registry database. Example 12-17 demonstrates the module.
Example 12-17. Using the _winreg Module
File: winreg-example-1.py
import _winreg explorer = _winreg.OpenKey( _winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\CurrentVersion\\Explorer" ) #list values owned by this registry key try: i = 0 while 1: name, value, type= _winreg.EnumValue(explorer, i) print repr(name), i += 1 except WindowsError: print value, type = _winreg.QueryValueEx(explorer, "Logon User Name") print print "user is", repr(value) 'Logon User Name' 'CleanShutdown' 'ShellState' 'Shutdown Setting' 'Reason Setting' 'FaultCount' 'FaultTime' 'IconUnderline'... user is u'Effbot'
好了這篇文章就先介紹到這了
相關(guān)文章
python實(shí)現(xiàn)合并兩個(gè)有序列表的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)合并兩個(gè)有序列表的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Python量化交易實(shí)戰(zhàn)之使用Resample函數(shù)轉(zhuǎn)換“日K”數(shù)據(jù)
resample函數(shù)是Python數(shù)據(jù)分析庫Pandas的方法函數(shù),它主要用于轉(zhuǎn)換時(shí)間序列的頻次,今天通過本文給大家分享python使用Resample函數(shù)轉(zhuǎn)換時(shí)間序列的相關(guān)知識(shí),感興趣的朋友一起看看吧2021-06-06
在keras中實(shí)現(xiàn)查看其訓(xùn)練loss值
這篇文章主要介紹了在keras中實(shí)現(xiàn)查看其訓(xùn)練loss值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
python xmind 包使用詳解(其中解決導(dǎo)出的xmind文件 xmind8可以打開 xmind2020及之后版本打
xmind8 可以打開xmind2020 報(bào)錯(cuò),如何解決這個(gè)問題呢?下面小編給大家?guī)砹藀ython xmind 包使用(其中解決導(dǎo)出的xmind文件 xmind8可以打開 xmind2020及之后版本打開報(bào)錯(cuò)問題),感興趣的朋友一起看看吧2021-10-10
python實(shí)現(xiàn)鍵盤控制鼠標(biāo)移動(dòng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)鍵盤控制鼠標(biāo)移動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10

