0

I am getting the error shown below. It looks like a path issue because of space.

>>> from _winreg import *
>>> aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE)
>>> print aReg
<PyHKEY at 03216070 (000001C8)>

>>> hKey = OpenKey(aReg, r"SOFTWARE\\Microsoft\\Windows\\Windows Error Reporting\\LocalDumps")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
WindowsError: [Error 2] The system cannot find the file specified

It is getting up to "Windows" correctly.

>>> hKey = OpenKey(aReg, r"SOFTWARE\\Microsoft\\Windows\\")
>>> print hKey
<PyHKEY at 03216050 (000001A0)>
Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
  • 1
    Don't double backslashes in a raw string. You can't end a raw string on a single backslash. – Eryk Sun Apr 13 '17 at 15:57
  • If you've used winreg.exe or reg.exe to confirm that the path exists, then probably you're using 32-bit Python. Try setting the access to explicitly open the 64-bit key: `hKey = OpenKey(HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps", 0, KEY_READ | KEY_WOW64_64KEY)`. – Eryk Sun Apr 13 '17 at 16:06
  • 1
    Possible duplicate of [Why does the single backslash raw string in Python cause a syntax error?](http://stackoverflow.com/questions/30283082/why-does-the-single-backslash-raw-string-in-python-cause-a-syntax-error) – ivan_pozdeev Apr 13 '17 at 19:04

1 Answers1

1

Don't use both raw strings and escape backslashes. They are two alternative ways to express the same thing: "a\\b == r"a\b". So, either

r'SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps'

or

'SOFTWARE\\Microsoft\\Windows\\Windows Error Reporting\\LocalDumps'

On my machine this works without error:

from _winreg import *
aReg = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
hKey = OpenKey(aReg, r'SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps')

but I had to add the key LocalDumps first, because it wasn't there.

BoarGules
  • 16,440
  • 2
  • 27
  • 44