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.

To get the directory of the script being run, we write:

import pathlib

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

Then p is something like '/home/runner/ShockingRosyGui'.

To get the current working directory, we write:

import pathlib

p = pathlib.Path().resolve()
print(p)

We call resolve on the Path instance to return the current working directory.

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 *