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 try
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)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I think os.path.expanduser(path) could be helpful.
On Unix and Windows, return the argument with an initial component of
~or~userreplaced 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 modulepwd. An initial~useris 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
~useris 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')
Method 2
from pathlib import Path str(Path.home())
works in Python 3.5 and above. Path.home() returns a Path object providing an API I find very useful.
Method 3
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
Method 4
home_folder = os.getenv('HOME')
This should work on Windows and Mac OS too, works well on Linux.
Method 5
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?
Method 6
For windows;
import os
homepath = os.path.expanduser(os.getenv('USERPROFILE'))
will give you a handle to current user’s home directory and
filepath = os.path.expanduser(os.getenv('USERPROFILE'))+'\Documents\myfile.txt'
will give you a handle to below file;
C:UsersurUserNameDocumentsmyfile.txt
Method 7
I realize that this is an old question that has been answered but I thought I would add my two cents. The accepted answer was not working for me. I needed to find the user directory and I wanted it to work with and without sudo. In Linux, my user directory is “/home/someuser” but my root directory is “/root/”. However, on my Mac, the user directory is “/Users/someuser”. Here is what I ended up doing:
_USERNAME = os.getenv("SUDO_USER") or os.getenv("USER")
_HOME = os.path.expanduser('~'+_USERNAME)
This worked both with and without sudo on Mac and Linux.
Method 8
get (translated) user folder names on Linux:
from gi.repository import GLib docs = GLib.get_user_special_dir(GLib.USER_DIRECTORY_DOCUMENTS) desktop = GLib.get_user_special_dir(GLib.USER_DIRECTORY_DESKTOP) pics = GLib.get_user_special_dir(GLib.USER_DIRECTORY_PICTURES) videos = GLib.get_user_special_dir(GLib.USER_DIRECTORY_VIDEOS) music = GLib.get_user_special_dir(GLib.USER_DIRECTORY_MUSIC) downloads = GLib.get_user_special_dir(GLib.USER_DIRECTORY_DOWNLOAD) public = GLib.get_user_special_dir(GLib.USER_DIRECTORY_PUBLIC_SHARE) templates = GLib.get_user_special_dir(GLib.USER_DIRECTORY_TEMPLATES) print(docs) print(desktop) print(pics) print(videos) print(music) print(downloads) print(public) print(templates)
Method 9
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, …
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0