- Python 3: User’s OS Home Directory Path
- File Inside Home Directory
- ~ Substitution
- Using os.path.expanduser
- Home Directory Path
- File Inside Home Directory
- ~ Substitution
- References
- How to get the home directory in Python
- Use os module to get the Home Directory
- Use the pathlib module to get the Home Directory
- Conclusion
- How to find the real user home directory using Python?
- Using os module
- Example — Home Directory Path
- Output
- Example — File Inside Home Directory
- Output
- Example — ~ Substitution
- Output
- Using pathlib module
- Example — Home Directory Path
- Output
- Example — File Inside Home Directory
- Output
- Example — ~ Substitution
- Output
- How to retrieve the Desktop path in Windows with Python
Python 3: User’s OS Home Directory Path
On Python 3.5 and higher, you can get the path of the user’s operating system (OS) home directory by using the built-in pathlib library:
from pathlib import Path home_dir = Path.home() print( f'Path: < home_dir >!' )
In the code above, home_dir is an object of class PosixPath (if you’re running on Linux or macOS) or WindowsPath (if you’re running on Windows). You can print it directly (including inside formatted f-strings as in the example above), but if you want to perform string operations on it, you must first convert it to a string:
from pathlib import Path home_dir = Path.home() home_dir_str = str( home_dir )
File Inside Home Directory
You can also easily build paths inside the user’s home directory using .joinpath() . For example, to build a path representing ~/Documents/myfile.txt (a file named myfile.txt inside the Documents subdirectory in the user’s home directory):
from pathlib import Path filepath = Path.home().joinpath( 'Documents', 'myfile.txt' ) print( filepath )
~ Substitution
If you already have a string path like ~/Documents/myfile.txt where you want to replace ~ with the home directory path, you can use .expanduser() :
from pathlib import Path filepath_str = '~/Documents/myfile.txt' filepath = Path( filepath_str ).expanduser() print( filepath )
Using os.path.expanduser
Home Directory Path
An alternative to pathlib that is available on all Python 3 versions is the built-in os.path library. You can use os.path.expanduser to get the home directory of the current user:
import os home_dir = os.path.expanduser( '~' ) print( home_dir )
File Inside Home Directory
To build the path ~/Documents/myfile.txt , you can use os.path.join :
import os home_dir = os.path.expanduser( '~' ) filepath = os.path.join( home_dir, 'Documents', 'myfile.txt' ) print( filepath )
~ Substitution
The safe way to build paths is using os.path.join() , but if you already have a string path like ~/Documents/myfile.txt where you want to replace ~ with the home directory path, you can just pass it in directly to .expanduser() :
import os filepath = os.path.expanduser( '~/Documents/myfile.txt' ) print( filepath )
On Windows, the above code outputs C:\Users\jlee/Documents/myfile.txt , where the slashes are not uniform (i.e., incorrect). To avoid this problem (i.e., for portable code), you should use os.path.join , which automatically chooses the correct file path slashes for the system you’re currently using.
References
How to get the home directory in Python
In this article, we will learn how to get the path of the home directory in Python. We will use two built-in functions to get the home directory.
The home directory contains multiple files for a given user of the system. Look at the two below scripts to get the home directory in Python. We will look at two different modules of Python such as the os module and pathlib module.
Use os module to get the Home Directory
os module provides os.path.expanduser(‘~’) to get the home directory in Python. This also works if it is a part of a longer path like ~/Documents/my_folder/. If there is no ~ in the path, the function will return the path unchanged. This function is recommended because it works on both Unix and Windows. It returns the argument with an initial component of (tilt) ~ or ~user replaced by the user’s home address.
import os print(os.path.expanduser('~'))
Use the pathlib module to get the Home Directory
The pathlib module provides path.home() to get the home directory in Python. This function works fine if your Python version is Python 3.4+. It returns a new path object having the user’s home directory.
from pathlib import Path print(Path.home())
Conclusion
In this article, we learned two different ways to get the home directory of a user’s system in Python. One way was using os.path.expanduser(‘~’) and another way was pathlib.Path.home() . Do check your script to avoid errors.
How to find the real user home directory using Python?
On a multiuser operating system, a home directory is a file system location that holds the files specific to a particular user.
A login directory is another name for a home directory.You may obtain the home directory using Python in a number of ways.
Using os module
The os.path.expanduser() function in Python provides the most straightforward method for retrieving the user home directory across all platforms. The Python os module offers os.path.expanduser(«) to retrieve the home directory. This also functions if it is a part of a longer path. The function will return the path unchanged if there is no ~ in the path.
Example — Home Directory Path
Following is an example to find the home directory using os.path.expanduser() function −
import os home_directory = os.path.expanduser( '~' ) print( home_directory )
Output
Following is an output of the above code −
Example — File Inside Home Directory
Use os.path.join to create the path C:\Users\Lenovo\Downloads\Works −
import os home_directory = os.path.expanduser( '~' ) path = os.path.join( home_directory, 'Documents', 'mysql_access' ) print( path )
Output
Following is an output of the above code −
C:\Users\Lenovo\Documents\mysql_access
Example — ~ Substitution
If you already have a string path, such as C:\Users\Lenovo\Downloads\Works, where you wish to replace with the home directory path, you can put it in directly to.expanduser() instead of using the safe way to generate paths, which is os.path.join() −
import os path = os.path.expanduser('~\Documents\mysql_access') print( path )
Output
Following is an output of the above code −
C:\Users\Lenovo\Documents\mysql_accessy
Using pathlib module
The pathlib module in Python can also be used to obtain the user’s home directory.
Example — Home Directory Path
Following is an example to find the home directory path.home() function −
from pathlib import Path home_directory = Path.home() print( f'Path: home_directory> !' )
Output
Following is an output of the above code −
Example — File Inside Home Directory
Using.joinpath, you can also quickly create paths inside the user’s home directory () −
from pathlib import Path path = Path.home().joinpath( 'Documents', 'mysql_access' ) print(path)
Output
Following is an output of the above code −
C:\Users\Lenovo\Documents\mysql_access
Example — ~ Substitution
Use.expanduser() if you already have a string path, such as /Documents/mysql_access, where you want to replace with the path to your home directory −
from pathlib import Path path_string = '~\Documents\mysql_access' path = Path(path_string).expanduser() print(path)
Output
Following is an output of the above code −
C:\Users\Lenovo\Documents\mysql_access
How to retrieve the Desktop path in Windows with Python
Learn how to retrieve the Desktop path in Windows using the Python programming language.
In order to retrieve the desktop directory with Python, you can built it based on the concatenation of the USERPROFILE environment variable of Windows with the OS module of Python. As you may know, the OS module provides a portable way of using operating system dependent functionality. Of our interest, the submodule of our interest is the os.path that allow you to manipulate paths on the system and the os.environ , known as a mapping object that returns a dictionary of the user’s environmental variables.
To get started, we’ll explain step by step what every piece of code means. The initial step is to obtain the current users directory, this can be obtained through the USERPROFILE environment variable of Windows, with python, use os.environ function to retrieve it:
import os # Prints the current user's directory: C:\Users\sdkca print(os.environ['USERPROFILE']) # Or print(os.environ.get("USERPROFILE"))
Knowing this, you can automatically assume the Desktop directory of the user appending the Desktop keyword at the end of the path obtained from the mentioned environment variable. This can be made in a single line of code:
import os desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop') # Prints: C:\Users\sdkca\Desktop print("The Desktop path is: " + desktop)