- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How to get the home directory in Python?
To get the homedir in python, you can use os.path.expanduser('~') from the os module. This also works if its a part of a longer path like ~/Documents/my_folder/. If there is no ~ in the path, the function will return the path unchanged. You can use it like −
import os print(os.path.expanduser('~'))
You can also query the environment variables for the HOME variable −
import os print(os.environ['HOME'])
- Related Articles
- Get Java Home Directory
- How to find the real user home directory using Python?
- How do I get the parent directory in Python?
- How to Change the Default Home Directory of a User on Linux?
- Golang Program to print the home directory of the current user
- How to get the current username and directory in Golang?
- How to get root directory information in android?
- How to get a list of all sub-directories in the current directory using Python?
- How to get full path of current file's directory in Python?
- How to get system data directory information in android?
- How to set the current working directory in Python?
- How to Get the most recent file in a directory in Linux?
- How do you get a directory listing sorted by creation date in Python?
- How do you get a directory listing sorted by their name in Python?
- How to rename directory using Python?