使用 Python(winreg)訪問 Windows 登錄檔


作為一門通用語言,並且有大量使用者支援的模組可用,我們發現 Python 也擅長 OS 級別程式設計。在本文中,我們將瞭解 Python 如何訪問 Windows 作業系統的登錄檔。

我們需要將名為 winreg 的模組匯入到 Python 環境中。

在下面的示例中,我們先使用 winreg 模組透過 ConnectRegistry 函式連線到登錄檔,然後使用 OpenKey 函式訪問登錄檔。最後,我們設計一個 for 迴圈來列印已訪問鍵的結果。

示例

import winreg
#connecting to key in registry
access_registry = winreg.ConnectRegistry(None,winreg.HKEY_LOCAL_MACHINE)

access_key = winreg.OpenKey(access_registry,r"SOFTWARE\Microsoft\Windows\CurrentVersion")
#accessing the key to open the registry directories under
for n in range(20):
   try:
      x =winreg.EnumKey(access_key,n)
      print(x)
   except:
      break

輸出

執行上述程式碼後將得到以下結果

ApplicationFrame
AppModel
Appx
Audio
Authentication
AutoRotation
BITS
Casting
ClosedCaptioning
CloudExperienceHost
Component Based Servicing
……..
…..

更新日期:2020-01-07

5K+ 瀏覽量

開啟您的職業生涯

完成課程,取得認證

開始
廣告