'The results of python's work should be written to a text file, python 3
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")

Solution 1:[1]
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hannes Ziegler |
