Помогите с python кодом работающим с reg файлами

Рейтинг: 0Ответов: 1Опубликовано: 26.07.2025
def reg_tweak_opener(reg_file):
    def parse_reg_file(file_path):
        try:
            with open(file_path, 'r') as file:
                lines = file.readlines()
            print(f"Файл {file_path} успешно открыт.")
        except Exception as e:
            print(f"Не удалось открыть файл {file_path}: {e}")
            return []

        reg_entries = []
        current_key = None

        for line in lines:
            line = line.strip()
            if line.startswith('[') and line.endswith(']'):
                current_key = line[1:-1]
            elif '=' in line and current_key:
                key_value = line.split('=', 1)
                value_name = key_value[0].strip().strip('"')
                value_data = key_value[1].strip().strip('"')
                reg_entries.append((current_key, value_name, value_data))

        return reg_entries

    def write_to_registry(entries):

        root = Tk()
        root.withdraw()

        if messagebox.askyesno("Подтверждение", "Вы действительно хотите продолжить?"):
            for key_path, name, value in entries:
                try:
                    with winreg.CreateKey(winreg.HKEY_CURRENT_USER, key_path) as key:
                        if isinstance(value, str):
                            winreg.SetValueEx(key, name, 0, winreg.REG_SZ, value)
                    print(f'Successfully added: {name} to {key_path}')
                except Exception as e:
                    print(f'Error adding {name} to {key_path}: {e}')
        else:
            print("Операция отменена.")

        root.destroy()

    reg_file_path = reg_file
    reg_entries = parse_reg_file(reg_file_path)
    if reg_entries:
        write_to_registry(reg_entries)

почему именно этот reg файл не вызывает wtite_to_registry ,а остальные нормально работают :

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile\Tasks\Games]
"GPU Priority"="8"
"Priority"="6"
"Scheduling Category"="High"

Ответы

▲ 2

Потому что в коде идёт работа только с веткой HKEY_CURRENT_USER:

with winreg.CreateKey(winreg.HKEY_CURRENT_USER, key_path) as key

А в файле используется ветка HKEY_LOCAL_MACHINE