I'm trying to write into the Windows registry key "SOFTWARE/Microsoft/DirectX/UserGpuPreferences" using the Python winreg module.
The name of the sub-key needs to be the python executable path, which contains backslashes. However when using winreg.SetValue()
, this instead adds a tree of keys following the components of the path, instead of a single sub-key whose name contains backslashes.
import winreg
path = ['SOFTWARE', 'Microsoft', 'DirectX', 'UserGpuPreferences']
registry = winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER)
key = winreg.OpenKey(registry, '\\'.join(path), 0, winreg.KEY_WRITE)
sub_key = sys.executable
value = 'GpuPreference=2;'
winreg.SetValue(key, sub_key, winreg.REG_SZ, value)
Also escaping the backslashes (sys.executable.replace('\\', '\\\\')
) does not change it.
Is there any way to insert such a registry value, using Python?
It works correctly with SetValueEx()