[python-win32] Finding users home directories
Mark Hammond
mhammond at skippinet.com.au
Mon Jan 14 00:39:21 CET 2008
> it's possible, but not slick. If you wanted to follow their
> line and use WMI to access the registry, you could additionally
> use the WMI Win32_UserAccount class to work out the SID you need.
> For example, to find my profile on this machine, the following
> seems to work:
>
> (uses the wmi module from http://timgolden.me.uk/python/wmi.html)
>
> <code>
> import _winreg
> import win32api
> import wmi
>
> #
> # Use the current username in DOM\USER format
> #
> USERNAME = win32api.GetUserNameEx (2)
> ## USERNAME = "GOYLE\\tim"
>
> HKLM = 0x80000002
> profiles_key = r"SOFTWARE\Microsoft\Windows
> NT\CurrentVersion\ProfileList"
>
> c = wmi.WMI (find_classes=False)
> for account in c.Win32_UserAccount (Caption=USERNAME):
> sid = account.SID
> break
> else:
> raise Exception, "User %s not found" % USERNAME
FYI, note that getting a SID is as simple as:
>>> import win32security
>>> user_sid = win32security.LookupAccountName(None, "Administrator")[0]
>>> str(user_sid)
'PySID:S-1-5-21-...'
>>>
(I'm not currently logged in as Administrator)
Cheers,
Mark
More information about the python-win32
mailing list