Sometimes, we want to determine the current script directory with Python.
In this article, we’ll look at how to determine the current script directory with Python.
How to determine the current script directory with Python?
To determine the current script directory with Python, we can use the os.path.dirname
method.
For instance, we write
os.path.dirname(os.path.abspath(__file__))
to get the current script’s path with __file__
.
Then we get the absolute path of the script with os.path.abspath
.
And then we get the folder name from the absolute path with os.path.dirname
.
Conclusion
To determine the current script directory with Python, we can use the os.path.dirname
method.