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("/foo.txt")
print(p)
We call os.path.abspath
with the relative path to the file and returns the absolute path.
Then we get that p
is something like '/foo.txt'
.
Conclusion
To get an absolute file path in Python, we can use the os.path.abspath
method.