python讀取注冊表中值的方法
在Python的標準庫中,_winreg.pyd可以操作Windows的注冊表,另外第三方的win32庫封裝了大量的Windows API,使用起來也很方便。不過這里介紹的是使用_winreg操作注冊表,畢竟是Python自帶的標準庫,無需安裝第三方庫。
下面的例子是通過Python獲取Windows XP下已經(jīng)安裝的補丁號。Windows的補丁號都在“HKEY_LOCAL_MACHINE\SOFTWARE\\Microsoft\\Updates”下,通過循環(huán)下面所有的目錄節(jié)點,如果找到的名稱符合正則表達式KB(\d{6}).*,則表示是一個補丁號。
從例子可以看出操作起來非常的簡單和快速。
# -*- coding: utf-8 -*-
# 獲取Windows的已打的補丁號
from _winreg import *
import re
def subRegKey(key, pattern, patchlist):
# 個數(shù)
count = QueryInfoKey(key)[0]
for index in range(count):
# 獲取標題
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量化交易實戰(zhàn)之使用Resample函數(shù)轉(zhuǎn)換“日K”數(shù)據(jù)
resample函數(shù)是Python數(shù)據(jù)分析庫Pandas的方法函數(shù),它主要用于轉(zhuǎn)換時間序列的頻次,今天通過本文給大家分享python使用Resample函數(shù)轉(zhuǎn)換時間序列的相關(guān)知識,感興趣的朋友一起看看吧2021-06-06
python xmind 包使用詳解(其中解決導出的xmind文件 xmind8可以打開 xmind2020及之后版本打
xmind8 可以打開xmind2020 報錯,如何解決這個問題呢?下面小編給大家?guī)砹藀ython xmind 包使用(其中解決導出的xmind文件 xmind8可以打開 xmind2020及之后版本打開報錯問題),感興趣的朋友一起看看吧2021-10-10

