Sometimes, we want to get an absolute file path in Python.
In this article, we’ll look at how to get an absolute file path in Python.
How to get an absolute file path in Python?
To get an absolute file path in Python, we can use the os.path.abspath
method.
For instance, we write
import os
p = os.path.abspath("mydir/myfile.txt")
to call os.path.abspath
with a relative path.
And then the absolute path equivalent of the relative path is returned.
Conclusion
To get an absolute file path in Python, we can use the os.path.abspath
method.