Python winreg using sub-keys

I can successfully extract 5 sub-keys from my Windows 7 registry hive "HKEY_LOCAL_MACHINE" with the code below.

from _winreg import * try: i = 0 while True: subkey = EnumKey(HKEY_LOCAL_MACHINE, i) print subkey i += 1 except WindowsError: pass 

My question is: how can I list the keys under these? I want to finish listing all the keys in the SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ NetworkList \ Signatures \ Unmanaged folder, but I can’t figure out what to do there.

In response to the first comment, I ran this code on my machine, and although it did not exit the system, it did not produce results.

 from _winreg import * aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE) aKey = OpenKey(aReg, r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Signatures\Unmanaged") for i in range(1024): try: keyname = EnumKey(aKey, i) asubkey = OpenKey(aKey, keyname) val = QueryValueEx(asubkey, "Description") print val except WindowsError: break 

The regedit or reg query shows 6 values ​​in this folder, but I cannot get a python script to show me these six.

+7
source share
5 answers

I do not have the same registry keys to search, but the following code will display all subkeys in HKEY_LOCAL_MACHINE \ Software. I think that if you change the value of the keyVal string in your directory, it will work.

The try ... except block is because EnumKey will fail. I did not do this as a for loop because I do not know how to get the correct aKey length.

 keyVal = r"Software" aKey = OpenKey(HKEY_LOCAL_MACHINE, keyVal, 0, KEY_ALL_ACCESS) try: i = 0 while True: asubkey = EnumKey(aKey, i) print(asubkey) i += 1 except WindowsError: pass 
+1
source

Just want to add a possibly more pythonic solution.

 from _winreg import * from contextlib import suppress import itertools def subkeys(path, hkey=HKEY_LOCAL_MACHINE, flags=0): with suppress(WindowsError), OpenKey(hkey, path, 0, KEY_READ|flags) as k: for i in itertools.count(): yield EnumKey(k, i) 

You can now access the keys as expected

 for key in subkeys(r'path\to\your\key'): print key 

For versions of Python <3.4 that don't have suppress () , I recommend adding it to my project:

 from contextlib import contextmanager @contextmanager def suppress(*exceptions): try: yield except exceptions: pass 

Note. If you are having trouble reading certain values, you may be reading from a misrepresentation of the registry. Pass KEY_WOW64_64KEY or KEY_WOW64_32KEY to the flags parameter). Using OpenKey() as a context manager was introduced in Python 2.6 .

+1
source

Something like this work?

 import _winreg def subkeys(key): i = 0 while True: try: subkey = _winreg.EnumKey(key, i) yield subkey i+=1 except WindowsError: break def traverse_registry_tree(key=_winreg.HKEY_LOCAL_MACHINE, tabs=0): for k in subkeys(key): print '\t'*tabs + str(k) traverse_registry_tree(k, tabs+1) 
0
source

This works and prints a list of all subsections (fixed version of @Broseph's answer)

 import _winreg def subkeys(key): i = 0 while True: try: subkey = _winreg.EnumKey(key, i) yield subkey i+=1 except WindowsError as e: break def traverse_registry_tree(hkey, keypath, tabs=0): key = _winreg.OpenKey(hkey, keypath, 0, _winreg.KEY_READ) for subkeyname in subkeys(key): print '\t'*tabs + subkeyname subkeypath = "%s\\%s" % (keypath, subkeyname) traverse_registry_tree(hkey, subkeypath, tabs+1) keypath = r"SOFTWARE\\Microsoft\\Windows" traverse_registry_tree(_winreg.HKEY_LOCAL_MACHINE, keypath) 
0
source

To iterate using the Windows registry keys, you will need EnumKey() from _winreg . The following is the definition for EnumKey() : -

def EnumKey (key, index):

  • Enumerates the subkeys of the public registry key.
  • a key is already a public key or any of the predefined HKEY_ * constants.
  • index is an integer that identifies the index of the key to retrieve.

Note that this method takes an index as an argument and will provide you with a key only for that index. Therefore, to get all the keys, you need to increase the index by one and continue until you encounter a WindowsError .

Refer to this post for a detailed idea of ​​the same. The Github link for the code can be found in the post.

0
source

All Articles