How to get user home directory on Windows (original) (raw)
Giampaolo Rodola'
unread,
Jan 12, 2008, 11:31:59 AM1/12/08
to
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.
Christian Heimes
unread,
Jan 12, 2008, 11:44:40 AM1/12/08
Giampaolo Rodola' wrote:
> Is there a way to do that?
home = os.path.expanduser("~")
Christian
Giampaolo Rodola'
unread,
Jan 12, 2008, 11:49:37 AM1/12/08
to
That gives the home of the *current logged in user*.
I need another thing.
Giampaolo Rodola'
unread,
Jan 12, 2008, 12:50:16 PM1/12/08
to
Update.
I found a way for getting the home directory of the user but it
requires to validate the user by providing 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, the
same way as if I would on UNIX where it would be enough just using the
pwd module:
>>> import pwd
>>> pwd.getpwnam('user').pw_dir
'/home/user'
thebjorn
unread,
Jan 13, 2008, 10:01:49 AM1/13/08
to
Check out http://msdn2.microsoft.com/en-us/library/bb762181(VS.85).aspx
for some of the complexities of special directories on Windows.
If you give more details on what you need to get done, someone might
come up with a better solution (my instinct tells me this might be a
database problem, but then I'm a database person so that might not be
terribly relevant ;-)
-- bjorn
Tim Golden
unread,
Jan 13, 2008, 10:28:39 AM1/13/08
to thebjorn, pytho...@python.org
thebjorn wrote:
> On Jan 12, 6:50 pm, "Giampaolo Rodola'" <gne...@gmail.com> wrote:
>> Update.
>> I found a way for getting the home directory of the user but it
>> requires to validate the user by providing 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, the
>> same way as if I would on UNIX where it would be enough just using the
>> pwd module:
>>
>> >>> import pwd
>> >>> pwd.getpwnam('user').pw_dir
>> '/home/user'
>
> Check out http://msdn2.microsoft.com/en-us/library/bb762181(VS.85).aspx
> for some of the complexities of special directories on Windows.
Part of the problem here is that is the OP is asking for the
"home dir" of a user, but there is no such thing under Windows.
Or, rather, there are several such things. The code he posts
above will get the path to what will be the user's %USERPROFILE%
env var when he logs on. (Which is certainly one of the
definitions of "home dir"). But he wants that path *without* having
to log them in. The page you refer to (and other similar suggestions
of using the os.expanduser function which essentially does the same
thing for you) assume you're already logged in as the user in question.
I've posted a (WMI-based) solution [1] on the python-win32 list
where the OP copied his question. You could do the same just with
win32security and _winreg. (Left as an exercise... etc.) Haven't
yet heard back from the OP as to whether that's given him what he
wants or not.
TJG
[1] http://mail.python.org/pipermail/python-win32/2008-January/006656.html
Martin P. Hellwig
unread,
Jan 13, 2008, 8:21:28 PM1/13/08
to
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
Tim Golden
unread,
Jan 13, 2008, 10:02:38 PM1/13/08
The trouble with that is that any particular user's profile can be
specifically overridden. I'm not attached to an AD network here,
but I think a networked user's profile can be network based
(whike a local user's won't be, say). And there are other ways
to change the profile too, down to hacking the registry as
described here:
http://support.microsoft.com/kb/314843/#XSLTH3129121125120121120120
That said, it'll probably work for a lot of the people for
a lot of the time.
TJG
Lie
unread,
Jan 14, 2008, 2:09:43 PM1/14/08
to
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
Giampaolo Rodola'
unread,
Jan 14, 2008, 3:14:39 PM1/14/08
to
Thanks to Tim Golden suggestions I solved my problem.
...In case it would help someone:
import \_winreg
import win32security
username = 'Administrator'
sid = win32security.ConvertSidToStringSid(
win32security.LookupAccountName(None, username)[0]
)
key = _winreg.OpenKey(
_winreg.HKEY_LOCAL_MACHINE,
r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" + "\
\" + sid
)
value, type = _winreg.QueryValueEx(key, "ProfileImagePath")
print value