Categories
Python Answers

How to get an absolute file path in Python?

Spread the love

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.

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 *