Search code examples
pythonwinreg

Using winreg for getting GPU details


I'm using winreg in python 3.4 to get windows registry values. I already set up a system to get CPU informations (cpu name, max frequency, etc.) and i'm trying to adapt it to get GPU informations. The only problem is that the key that contain the GPU values is stored there: SYSTEM\CurrentControlSet\Control\Video{D1B33FF8-E663-44A7-9C71-2CE551F6C0EE}\0000

So here's my code line to get to the GPU "directory":

self.connection = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
self.subkey = winreg.OpenKey(self.connection, "SYSTEM\CurrentControlSet\Control\Video\{D1B33FF8-E663-44A7-9C71-2CE551F6C0EE}\0000")

But I get this error: "OpenKey() argument 2 must be str without null characters or None, not str" So my question is: How can I use null characters in the path ? Thanks a lot, Julien.


Solution

  • The backslash characters in the string are used as escape sequences in Python.

    The easiest way to use them as regular characters is to use a r-string:

    self.subkey = winreg.OpenKey(self.connection, r"SYSTEM\CurrentControlSet\Control\Video\{D1B33FF8-E663-44A7-9C71-2CE551F6C0EE}\0000")
    

    That is use r"..." instead of just "...". That way the backslash character is not used as an escape character.