views:

317

answers:

6

I see that if we change the HOME(linux) or USERPROFILE(windows) environmental variable and run a python script, it returns the new value as the user home when I tried, os.environ['HOME'] os.exp

Is there any way to find the real user home directory without relying on the environmental variable?. Thanx.
edit:
Here is a way to find userhome in windows by reading in the registry,
http://mail.python.org/pipermail/python-win32/2008-January/006677.html

edit:
One way to find windows home using pywin32,

from win32com.shell import shell,shellcon
home = shell.SHGetFolderPath(0, shellcon.CSIDL_PROFILE, None, 0)
A: 

On Linux and other UNIXoids you can always take a peek in /etc/passwd. The home directory is the sixth colon-separated field in there. No idea on how to do better than the environment variable on Windows though. There'll be a system call for it, but if it's available from Python, ...

calmh
But that would only be Unix(Linux) isn't it?
ring bearer
@ring - yep. Added text to that effect.
calmh
+1  A: 

Really, a change in environment variable indicates that the home must be changed. So every program/script should have the new home in context; also the consequences are up to the person who changed it. I would still stick with home = os.getenv('USERPROFILE') or os.getenv('HOME')

what exactly is required?

ring bearer
Except on Cygwin, in which case both are usually defined, and many times different.
amphetamachine
+7  A: 

I think os.path.expanduser(path) could be helpful.

On Unix and Windows, return the argument with an initial component of ~ or ~user replaced by that user‘s home directory.

On Unix, an initial ~ is replaced by the environment variable HOME if it is set; otherwise the current user’s home directory is looked up in the password directory through the built-in module pwd. An initial ~user is looked up directly in the password directory.

On Windows, HOME and USERPROFILE will be used if set, otherwise a combination of HOMEPATH and HOMEDRIVE will be used. An initial ~user is handled by stripping the last directory component from the created user path derived above.

If the expansion fails or if the path does not begin with a tilde, the path is returned unchanged.

So you could just do:

os.path.expanduser('~user')
Felix Kling
True, but changing the environment variable as in the question will "fool" this method as well. Now, why one would want to do that, I can't say. :)
calmh
@calmh: Yes, I changed it to use `'~user'` which should work on Linux and Windows (here I am not 100% sure because I don't have Windows to test it ;) ).
Felix Kling
This is working in Linux, But not in windows. In windows it just joins the "C:\Documents and settings" to the username passed.
asdfg
A: 

You may want to checkout unix command(shortcut): ~user It takes you to home directory of current user.

On windows have no idea.

Marek Mollin
+1  A: 

I think os.path.expanduser(path) is the best answer to your question, but there's an alternative that may be worth mentioning in the Unix world: the pwd package. e.g.

import os, pwd

pwd.getpwuid(os.getuid()).pw_dir
Brian M. Hunt
A: 

home_folder = os.getenv('HOME')

this should work on Windows and Mac OS too. Works fine on Linux.

Owais Lone