What is __file__ variable in Python?

What Is File Variable In Python

Python language combines different Built-in functions, Built-in methods, and special variables. Let’s understand Python’s __file__ variable in detail. These functions and variables make Python language easy and more understandable.

There are different Built-in functions, Built-in methods, and special functions in Python which carry a special meaning and functionality with them. Let’s take an example of the map() function, which is a built-in function in Python. This function is used when we need mapping to solve the problem. So, every built-in function has its role and functionality. Similarly, Python language provides one more category for solving complex problems efficiently. This is referred to as a dunder in Python.

In Python, let’s unearth the concept behind the __file__ variable.

What is Dunder in Python?

The __file__ variable is also known as a dunder in Python. All the special variables with a double underscore as a ‘suffix’ and ‘prefix’ are considered as a dunder variable in Python. These dunder variables are also known as magic methods in Python.

The magic methods include many special variables like __init__, __add__ , __repr__, __file__() and many more. Every magic method has different functionality. For example, __init__ works as a constructor of a class in Python, __add__ is used to make the sum of two different objects and __repr__ method is used to represent the object. Let’s see the __file__ variable/ method.

__File__ Variable in Python

The __file__ is a special variable in Python that contains the module’s path. In Python, we use modules to perform various tasks and solve complex problems. At the beginning of programs, we need to install modules and packages to use the functions. So, this module has its path where it is stored. This __file__ variable in Python helps to hold this path of the module which is imported. First, we need to understand the modules in Python.

Module in Python

The module is a file consisting of Python code. These modules contain various functions, classes, and objects. We can refer to these modules to solve our problems. For example, The numpy package of Python is used to perform numerical operations on the data. This numpy module mainly deals with the arrays. There are different functions in this module like ‘.array()’, ‘.random()’, and many more.

Sometimes, we need to know the path of this module to use it as a reference in another file. For this we need __file__ variable in Python.

Implementation of __file__ variable in Python

The __file__ variable mainly stores the path of the module, so we can consider it a storage variable. This storage variable is created every time by the import system of Python when we import a module in our code. The output of this __file__ variable is a string because it returns the path of the module. This variable is empty when there is no such module that is imported.

Let’s see the example of the __file__ variable to understand the working.

Example 1: Implementation of __file__ Variable

import os
print(f"__file__, {__file__}")



Here, in this example, the ‘os’ module is imported. Inside the print statement, the __file__ variable is used to print the path of the module. In 4th line of code, the target.txt file is also attached to see the detailed path of the ‘os’ module. Let’s see the output.

File Variable In Python
File Variable In Python

The last line of the output shows the detailed path of the target.txt file. The os module is used in the ‘pythonProject1’ folder. So, in this way, we can use the __file__ variable to know the path of any module which is imported.

Example 2: Find a Path of User-Defined Module

In example 2, first, we need to define the module. Here, 2 files are created main.py and module.py. The module.py file contains the module and contains the hello() function. This module is imported into the main.py file. Let’s see the code and implementation in detail.

Module.py File

import module

Here, the module is imported from the module.py file. The __file__ variable is used to print the path. The hello() function is used to print the sentence.

Main.py File

def hello():
    print("This is imported from module file")

In this main.py file, the hello() function is initialized to print the sentence.


Output Of File Variable
Output Of File Variable

This output shows the path where the statement is also printed. This shows the working of the __file__ variable.


In this article, the __file__ variable, which is a special variable in Python explained in detail. The __file__ variable is used to store the path of the module in Python. Here, the two examples are implemented to understand the working. The first example is based on the module, which is already defined, and in the second example the module is initialized, and the function is used in another file. In this way, the working of the __file__ variable is explained in detail. I hope you enjoy the article.


Do read the official documentation of modules in Python.