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("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.

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 *