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?
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)
get (translated) user folder names on Linux:
I think
os.path.expanduser(path)
could be helpful.So you could just do:
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: thepwd
package. e.g.works in Python 3.5 and above.
Path.home()
returns aPath
object providing an API I find very useful.For windows;
will give you a handle to current user's home directory and
will give you a handle to below file;
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?