最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python讀取注冊表中值的方法

 更新時間:2013年04月08日 17:12:56   投稿:shangke  
在Python的標準庫中,_winreg.pyd可以操作Windows的注冊表,另外第三方的win32庫封裝了大量的Windows API,使用起來也很方便。不過這里介紹的是使用_winreg操作注冊表,畢竟是Python自帶的標準庫,無需安裝第三方庫

在Python的標準庫中,_winreg.pyd可以操作Windows的注冊表,另外第三方的win32庫封裝了大量的Windows API,使用起來也很方便。不過這里介紹的是使用_winreg操作注冊表,畢竟是Python自帶的標準庫,無需安裝第三方庫。

下面的例子是通過Python獲取Windows XP下已經安裝的補丁號。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) 

下面內容轉自  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'

好了這篇文章就先介紹到這了

相關文章

最新評論

南丹县| 荆门市| 田阳县| 日喀则市| 丹江口市| 晴隆县| 措勤县| 仪陇县| 股票| 德保县| 南陵县| 巫溪县| 平塘县| 云和县| 丹阳市| 扶余县| 临清市| 鄂伦春自治旗| 滕州市| 婺源县| 郑州市| 惠州市| 双辽市| 和硕县| 永州市| 永清县| 奎屯市| 荥阳市| 大新县| 陇西县| 石河子市| 乐平市| 商城县| 安顺市| 和田市| 公安县| 思茅市| 贡嘎县| 平谷区| 东海县| 体育|