__file__ (A Special Variable) in Python
What are Special variables?
In Python, there are some variables which are defined using double underscores, and they are called Dunder or magic methods. They are used for special purposes in the code, and that is why they are referred to as special variables.
For example: __init__ , __add__ ,__len__ ,__file__ etc.
This method is called without any function call, and it is invoked when we create any object or instance of any class. In other programming languages, it is called the constructor.
In Python code, we import many files or modules in the current code to reuse the same code and to increase the efficiency. This method contains the path of the module which we have imported into our file for some special purpose. The type of this variable would be a string, and it can be empty or non-empty. If the module which we have imported is present in the database, not in our system then there would not be any path of that module, and this variable would be empty.
If the module is present in our local system then there would definitely be a path of the module, and it will be stored in this variable. The value of this variable would be determined by the import system of python. We will see some examples to understand the different use case scenarios for this variable.
In the above code, we have one module named test.py, where we have defined a method which will print some statements. Then we created a second module named test2.py, in which we have imported the test.py module. Now we use the method of test.py in test2.py, and we print the __file__ variable of the first module.
We got the path of the test.py module using this special variable.
We will import more than one module in the same file.
In the above code, we have three different modules named as test1,test3, and test4, where we have defined some methods. We have another module which is the main module or the file where we have imported all of the three modules. Now we have used the __file__ special variable for each module to get the path of each respective module.
If the modules are present in different folders:
In the above code, we have one module named add.py, which has the method to add the two numbers. We have one main module named test2.py, in which we have imported the add module, and then we have used the addNumber method of the add module in the current module.
We have used the __file__ special variable for the add module, which will print the exact path of the add module.
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week