User Home Dir
os.path
You want to use os.path.expanduser.
This will ensure it works on all platforms:
from os.path import expanduser
home = expanduser("~")
pathlib.Path
If you're on Python 3.5+ you can use pathlib.Path.home():
from pathlib import Path
home = str(Path.home())
But it's usually better not to convert Path.home()
to string. It's more natural to use this way:
with open(Path.home() / ".ssh" / "known_hosts") as f:
lines = f.readlines()
Last updated