[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

How to get user home directory on Windows

Giampaolo Rodola'

1/12/2008 4:32:00 PM

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.
14 Answers

Christian Heimes

1/12/2008 4:45:00 PM

0

Giampaolo Rodola' wrote:
> Is there a way to do that?

home = os.path.expanduser("~")

Christian

Giampaolo Rodola'

1/12/2008 4:50:00 PM

0

On 12 Gen, 17:44, Christian Heimes <li...@cheimes.de> wrote:
> Giampaolo Rodola' wrote:
> > Is there a way to do that?
>
> home = os.path.expanduser("~")
>
> Christian

That gives the home of the *current logged in user*.
I need another thing.

Giampaolo Rodola'

1/12/2008 5:50:00 PM

0

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

1/13/2008 3:02:00 PM

0

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/librar...(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

1/13/2008 3:29:00 PM

0

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/librar...(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/0...

Martin P. Hellwig

1/14/2008 1:21:00 AM

0

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

Tim Golden

1/14/2008 3:03:00 AM

0

Martin P. Hellwig 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'.

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/#XSLTH312912112512...

That said, it'll probably work for a lot of the people for
a lot of the time.

TJG

Lie Ryan

1/14/2008 7:10:00 PM

0

On Jan 14, 8:21 am, "Martin P. Hellwig" <x...@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

Giampaolo Rodola'

1/14/2008 8:15:00 PM

0

Thanks to Tim Golden suggestions I solved my problem.
....In case it would help someone:


<code>
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
</code>

The Starmaker

7/24/2014 6:19:00 PM

0

Bill Steele wrote:
>
> On 7/24/14, 12:52 PM, The Starmaker wrote:
> > Don't you people understand...politics?
>
> If you can't understand what "rec.arts.tv" means, how can we expect more?


Do you watch Star-Trek on TV?

http://www.youtube.com/watch?v=G...