January 26th, 2005, 09:19 AM
Writing to the Registry
Can any tell me how to write to the Registry in Python? I need to add my programs file name to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\run.
January 26th, 2005, 09:44 AM
Python contains a module named "_winreg" which allows you to manipulate the windows registry. For details see the current python documentation. For a working example of how to use it have a look at e.g. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146305.
January 26th, 2005, 12:14 PM
Thanks. That link helped a lot. I'm still trying to get it to add my program to a place other than Software, but it's better than nothings, thanks again.I think I figured it out, I know have:
This put my file in CurrentVersion\Run. This should work correctly. I'm just trying to change it's name from (Default) to PythonBot.
def __init__(self, company="Microsoft", project="Windows\CurrentVersion", write=1):
handle registry access
self.write = write
self.company = company
self.project = project
self.keyname = "Software\\%s\\%s" % (self.company, self.project)
self.key = wreg.OpenKey(wreg.HKEY_CURRENT_USER, self.keyname)
self.key = wreg.CreateKey(wreg.HKEY_CURRENT_USER, self.keyname)
def set(self, name, value):
" set value in registry "
if not self.write:
raise Exception, "registry is read only"
wreg.SetValue(self.key, name, wreg.REG_SZ,str(value))
def pset(self, name, value):
" set using pickle "
def get(self, name):
" get value out of registry "
return wreg.QueryValue(self.key, name)
def pget(self, name):
" get using pickle "
" close the key finally "
r = WindowsRegistry(project="Windows\CurrentVersion", write=1)
r.set("run", "C:\Documents and Settings\Master\Desktop\UOChat.py")