Categories
Python Answers

How to get the path and name of the file that is currently executing with Python?

Spread the love

Sometimes, we want to get the path and name of the file that is currently executing with Python.

In this article, we’ll look at how to get the path and name of the file that is currently executing with Python.

How to get the path and name of the file that is currently executing with Python?

To get the path and name of the file that is currently executing with Python, we can use the os.path.realpath method.

For instance, we write

import os

os.path.realpath(__file__)

to call os.path.realpath with __file__ to get the full path of the current script.

We get the actual path rather than the path of the symlink.

__file__ has the relative path of the current script.

Conclusion

To get the path and name of the file that is currently executing with Python, we can use the os.path.realpath 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 *