[python-win32] Finding users home directories

Tim Golden mail at timgolden.me.uk
Sat Jan 12 21:37:52 CET 2008


Giampaolo Rodola' wrote:
> I'm trying to use the pywin32 extension to find out the users home directories.
> Currently I found a way for doing that but it requires to validate the
> user by providing its username + password:
> 
> def get_homedir(username, password):
>    token = win32security.LogonUser(
>        username,
>        None,
>        password,
>        win32security.LOGON32_LOGON_NETWORK,
>        win32security.LOGON32_PROVIDER_DEFAULT
>        )
>    return win32profile.GetUserProfileDirectory(token)
> 
> 
> What I'd like to do is avoiding the requirement of the password, in
> the same way as if I would on UNIX where it would be enough just using
> the pwd module and providing the username only:
> 
>  >>> import pwd
>  >>> pwd.getpwnam('user').pw_dir
>  '/home/user'
> 
> Does someone know if it is possible to do that?

I thought it would be accessible via the win32net functions,
but it seems not. According to this page:

http://www.microsoft.com/technet/scriptcenter/resources/qanda/jun05/hey0603.mspx

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

registry = wmi.WMI (find_classes=False, namespace="default").StdRegProv
result, profile = registry.GetExpandedStringValue (
   _winreg.HKEY_LOCAL_MACHINE,
   profiles_key + "\\" + sid,
   "ProfileImagePath"
)

print USERNAME, "has profile at", profile
</code>

TJG


More information about the python-win32 mailing list