You want to use os.path.expanduser.
This will ensure it works on all platforms:
from os.path import expanduser
home = expanduser("~")
If you’re on Python 3.5+ you can use pathlib.Path.home():
from pathlib import Path
home = str(Path.home())
More Related Contents:
- What is a cross-platform way to get the home directory?
- How do you run a Python script as a service in Windows?
- Python: What OS am I running on?
- Monitoring contents of files/directories? [duplicate]
- Python: Platform independent way to modify PATH environment variable
- How to find the real user home directory using python?
- How to identify on which OS Python is running on?
- How to share conda environments across platforms
- How can I find the current OS in Python? [duplicate]
- Possible values from sys.platform?
- Cross-platform subprocess with hidden window
- How to add a timeout to a function in Python
- Regular Expression to match cross platform newline characters
- How can I create a ramdisk in Python?
- How to identify which OS Python is running on?
- Platform-independent file paths?
- Why does the calculated width and height in pixel of a string in Tkinter differ between platforms?
- Cross-platform way to get PIDs by process name in python
- Cross-platform gui toolkit for deploying Python applications
- Python multiprocessing linux windows difference
- How to format a floating number to fixed width in Python
- How to open a file using the open with statement
- How to allow or deny notification geo-location microphone camera pop up
- What is the current choice for doing RPC in Python? [closed]
- In pytest, what is the use of conftest.py files?
- Do we really need @staticmethod decorator in python to declare static method
- What are dict_keys, dict_items and dict_values?
- Pythonic way to avoid “if x: return x” statements
- What’s an example use case for a Python classmethod?
- In Python, when are two objects the same?