Python 3.5+pathlibfunction
pathlib.Path.home()
creation
Return the user's home directory as a Path; a classmethod called on Path itself rather than on an instance.
Syntax
Path.home()
The home directory comes from the environment (HOME on Unix, USERPROFILE on Windows), so inside a container, in CI or under a service account it can easily differ from what you see in your own shell; when it cannot be determined at all, RuntimeError is raised. Note also that pathlib never expands a tilde on its own — a ~ written inside a path string stays a literal directory name, and expanduser() is the method that resolves it.
Examples
from pathlib import Path
Path.home() # → PosixPath('/home/username')
Path.home() / 'Downloads' # → PosixPath('/home/username/Downloads')
Path.home().is_absolute() # → True
Path.home().is_dir() # → True
Path('~/notes.txt').expanduser() == Path.home() / 'notes.txt' # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy