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.