title: "WindowsError: [Error 5] Access is denied" _winreg.DeleteKey
body: <p>I am running a small chunk of code to auto-delete profiles from Windows PCs. Determining the usernames and deleting the folders works great, but I can't seem to get the reg keys deleted. I am running the .py in an elevated command prompt and I can't seem to find an answer that works. Bare in mind I'm pretty new to this. Thanks in advance!
  "dirs" is a list of usernames to be deleted. </p>
  
  <pre><code>#Determines OS, finds the SID, and makes it a string, then deletes the key
  keyloc = "Software\Microsoft\Windows NT\CurrentVersion\ProfileList"
  if opersys == "8":
      for kilist in dirs:
          sid = win32security.ConvertSidToStringSid(win32security.LookupAccountName(None, kilist)[0])
          key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keyloc, 0, _winreg.KEY_ALL_ACCESS)
          _winreg.DeleteKey(key, sid)
  </code></pre>