Working with the Windows Registry

Tim Roberts timr at probo.com
Sat Jun 28 08:13:04 CEST 2008


teh_sAbEr <teh.saber at gmail.com> wrote:

>Hi everybody. I'm trying to write a script that'll change desktop
>wallpaper every time its run. Heres what I've gotten so far:
>
>#random wallpaper changer!
>import _winreg
>from os import walk
>from os.path import exists
>from random import randint
>
>#first grab a registry handle.
>handle = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,'Control Panel
>\Desktop',_winreg.KEY_SET_VALUE)
>
>def GenerateListOfWallpapers():
>    targetDir = 'C:\Documents and Settings\Enrico Jr\My Documents\Jr
>\'s Wallpapers'

You are fortunate that your name is not "Tim" or "Ian" or "Nathan", because
this would not have worked as you have written it.

You either need to double the backslashes:
    ... 'C:\\Documents and Settings\\Enrico...'
or use forward slashes:
    ... 'C:/Documents and Settings/Enrico...'
or use the "r" modifier:
    ... r'C:\Documents and Settings\Enrico...'

However, as a general practice, it's probably better to get the special
directories from the environment:
    targetDir = os.environ['USERPROFILE'] + '\\My Documents\\Jr\'s
Wallpapers'

Remember that it's not called "Documents and Settings" on Vista...
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list