Menu Close

How to get the full path of the current file’s directory with Python?

Sometimes, we want to get the full path of the current file’s directory with Python.

In this article, we’ll look at how to get the full path of the current file’s directory with Python.

How to get the full path of the current file’s directory with Python?

To get the full path of the current file’s directory with Python, we can use pathlib.

For instance, we write

import pathlib
p = pathlib.Path(__file__).parent.resolve()

to get the full path of directory that the current script is in with

p = pathlib.Path(__file__).parent.resolve()

We can get the current working directory with

import pathlib

p = pathlib.Path().resolve()

Conclusion

To get the full path of the current file’s directory with Python, we can use pathlib.

Posted in Python, Python Answers