Categories
Python Answers

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

Spread the love

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 the pathlib module.

For instance, we write:

import pathlib

curr_path = pathlib.Path(__file__).parent.resolve()
print(curr_path)

We use the pathlib.Path constructor with __file__ to create a Path instance from the current script file.

Then we call parent.resolve to return the current path of the file and assign that to curr_path.

Therefore, curr_path is something like '/home/runner/GrandPoorField'.

Conclusion

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

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *