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 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.

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 *