Find “home directory” in Python? [duplicate]

To get the homedir in python, you can use os.path.expanduser('~').

This also works if it’s part of a longer path, such as os.path.expanduser('~/some/directory/file.txt'). If there is no ~ in the path, the function will return the path unchanged.

So depending on what you want to do it’s better than reading os.environ['HOME']

The username is available through getpass.getuser()

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)