How to get the home directory in Python?

Sometimes, we want to get the home directory in Python.

In this article, we’ll look at how to get the home directory in Python.

How to get the home directory in Python?

To get the home directory in Python, we can use the Path module.

For instance, we write

from pathlib import Path

home = str(Path.home())

to call Path.home method to return the home directory as a path object.

We then convert it to a string with str.

Conclusion

To get the home directory in Python, we can use the Path module.