How to create sub key in windows Registry Using Python winreg module?

393 views Asked by At

I want to add subkey in windows registry using python winreg. See the below picture for better understanding.

enter image description here

Now, I'm using this code.

import winreg as rg
regkey = rg.OpenKey(rg.HKEY_CLASSES_ROOT, "Directory\shell", 0,rg.KEY_WRITE)
rg.SetValueEx(regkey, "IFO/dr",0,rg.REG_SZ, r"F:\IFO\dist\rename_folder.exe")

I just want to create the subkey and than want to add value.

1

There are 1 answers

0
WillianKoessler On

To create a new sub_key, you need to use winreg.CreateKeyEx(key, sub_key, 0, access=KEY_CREATE_SUB_KEY), where key is the root HKEY_ (E.g. HKEY_LOCAL_MACHINE), and sub_key is the fullpath key you want to create.

import winreg
    
rootPath = r"Directory\shell"
keyPath = r"IFO\dr"

# Open/Create the sub_key
winreg.CreateKeyEx(winreg.HKEY_LOCAL_MACHINE, rootPath, 0, winreg.KEY_CREATE_SUB_KEY)
# Set its value
winreg.SetValue(winreg.HKEY_LOCAL_MACHINE, rootPath+'\\'+keyPath, winreg.REG_SZ,
                r"F:\IFO\dist\rename_folder.txt")