How to get user home directory on Windows

Lie Lie.1296 at gmail.com
Mon Jan 14 14:09:43 EST 2008


On Jan 14, 8:21 am, "Martin P. Hellwig" <x... at xs4all.nl> wrote:
> Giampaolo Rodola' wrote:
> > Hi all,
> > I'm trying to use the pywin32 extension to find out the user's home
> > directory but currently I didn't find a solution yet.
> > What I'd need to do is not getting the home directory of the currently
> > logged in user but something like:
>
> >>>> get_homedir("frank")
> > "C:\home\users\frank"
> >>>> get_homedir("josh")
> > "C:\home\users\josh"
>
> > Is there a way to do that?
> > I tried to search through the Pywin32 documentation with no luck.
> > In addition I'm not practiced with the Windows API at all.
>
> Well, windows, to my knowledge, uses the same base path for all profiles
> (this is not true for the My Documents folder which can differ). So what
> you could do is get the location from the ALLUSERPROFILE environment
> variable, go one folder higher and iterate through that.
> Ignoring the folders for the 'pseudo' users: 'All Users', 'Default
> User', 'LocalService' and 'NetworkService'.
>
> hth
> --
> mph

There is one problem with that, sometimes the folders for the users
are not the same with the user name, usually because of deleting user
name without deleting the profile, then recreate a user with similar
name. It doesn't happens everytime, but it could.

Possibly it is possible to get the SID of the user name (using the way
described in Tim Golden's Microsoft Link' post), then find the user
directory from the registry: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft
\Windows NT\CurrentVersion\ProfileList\[PUT SID HERE]\Profile Image
Path



More information about the Python-list mailing list