-2

I have a problem and I don't know how to solve it. There is a code in which I get the result of the found registry (its output) to the terminal. But, I need to write it to a file right away, and not write a command at startup. How can this be solved? Sorry, I'm still new to Python 3. Here's the code and foto of what I don't want to do. Thank you for your help.

from winreg import (
  ConnectRegistry,
  OpenKey,
  KEY_ALL_ACCESS,
  EnumValue,
  QueryInfoKey,
  HKEY_LOCAL_MACHINE,
  HKEY_CURRENT_USER
)
def enum_key(hive, subkey:str):
    with OpenKey(hive, subkey, 0, KEY_ALL_ACCESS) as key:
        num_of_values = QueryInfoKey(key)[1]
        for i in range(num_of_values):
            values = EnumValue(key, i)
            if values[0] == "LangID": continue
            print(*values[:-1], sep="\t")
if __name__ == "__main__":
    # Connecting to the HKEY_LOCAL_MACHINE hive
    with ConnectRegistry(None, HKEY_LOCAL_MACHINE) as hklm_hive:
        print("\nCurrent Version/Build Info")
        print("-"*50)
        enum_key(hklm_hive, r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon")
        
        

Jakob F
  • 1,046
  • 10
  • 23

1 Answers1

0

Do something like this:

def enum_key(hive, subkey:str):
    with OpenKey(hive, subkey, 0, KEY_ALL_ACCESS) as key:
        num_of_values = QueryInfoKey(key)[1]
        for i in range(num_of_values):
            values = EnumValue(key, i)
            if values[0] == "LangID": continue
            return (*values[:-1], sep="\t")
if __name__ == "__main__":
    # Connecting to the HKEY_LOCAL_MACHINE hive
    with ConnectRegistry(None, HKEY_LOCAL_MACHINE) as hklm_hive:
        with open(<filepath>, "w") as f:
            for line in enum_key(hklm_hive, r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon")            
                f.write(str(line))

Note: I didn't test this, read this more as pseudocode and look into open()