How to find real user home directory using python?

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

Is there a way to find the home directory of a real user without relying on an environment variable?

edit:
Here is a way to find userhome on 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) 
+58
python linux windows directory home-directory
Apr 19 '10 at 15:59
source share
9 answers

I think os.path.expanduser(path) might be useful.

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

On Unix, the source ~ is replaced with the HOME environment variable, if set; otherwise, the current user home directory is viewed in the password directory through the built-in pwd module. The initial ~user viewed directly in the password directory .

On Windows, HOME and USERPROFILE will be used if installed, otherwise a combination of HOMEPATH and HOMEDRIVE will be used. The initial ~user processed by removing the last directory component from the user path created above .

If expansion fails or if the path does not start with a tilde, the path returns unchanged.

So you can just do:

 os.path.expanduser('~user') 
+71
Apr 19 '10 at 16:05
source share

I think os.path.expanduser(path) is the best answer to your question, but there is an alternative that can be mentioned in the Unix world: pwd . eg.

 import os, pwd pwd.getpwuid(os.getuid()).pw_dir 
+11
Jun 21 '10 at 14:33
source share
 from pathlib import * str(Path.home()) 

works in Python 3.5 and higher. Path.home() returns a Path object that provides an API, which I find very useful.

+7
Jan 28 '17 at 22:24
source share

home_folder = os.getenv('HOME')

This should also work on Windows and Mac OS, works well on Linux.

+5
Jul 07 '10 at 14:18
source share

For windows;

 import os homepath = os.path.expanduser(os.getenv('USERPROFILE')) 

will provide you with a handle to the user's current home directory and

 filepath = os.path.expanduser(os.getenv('USERPROFILE'))+'\\Documents\\myfile.txt' 

will provide you with a handle to the file below;

 C:\Users\urUserName\Documents\myfile.txt 
+2
Dec 02 '15 at 12:44
source share

Indeed, a change in an environment variable indicates that the house should be changed. Therefore, each program / script must have a new home in context; also the consequences depend on the person who changed him. I will still stick with home = os.getenv('USERPROFILE') or os.getenv('HOME')

what exactly is required?

+1
Apr 19 '10 at 16:05
source share

I understand that this is an old question that was answered, but I thought I would add my two cents. The accepted answer did not work for me. I needed to find the user directory, and I wanted it to work with and without sudo . On 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 did:

 _USERNAME = os.getenv("SUDO_USER") or os.getenv("USER") _HOME = os.path.expanduser('~'+_USERNAME) 

This worked with and without sudo on Mac and Linux.

+1
Mar 12 '17 at 17:12
source share

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) 
0
Sep 23 '18 at 18:02
source share

On Linux and other UNIXoids, you can always look at /etc/passwd . The home directory is the sixth colon-separated field. I do not know how to do better than the environment variable in Windows. There will be a system call for this, but if it is available from Python, ...

-one
Apr 19 '10 at 16:04
source share



All Articles