Last updated: Apr 9, 2024
Reading timeยท4 min
Use the inspect.getfile()
method to get the file path of a class.
The getfile()
method takes an object and returns the name of the file in
which the object was defined.
from another import Employee import inspect import os # โ get the absolute file path of class # ๐๏ธ /home/borislav/Desktop/bobbyhadz_python/another.py print(inspect.getfile(Employee)) emp1 = Employee() # ๐๏ธ /home/borislav/Desktop/bobbyhadz_python/another.py print(inspect.getfile(emp1.__class__)) # ----------------------------------------------- # โ get the relative file path of class # ๐๏ธ another.py print(os.path.relpath(inspect.getfile(Employee)))
The example above assumes that you have an Employee
class in a file called
another.py
located in the same directory.
class Employee(): pass
The inspect.getfile() method returns the name of the file in which an object was defined.
from another import Employee import inspect # ๐๏ธ /home/borislav/Desktop/bobbyhadz_python/another.py print(inspect.getfile(Employee))
The getfile()
method throws a TypeError
if the object is a built-in module,
class or function.
If you only have access to an instance of the class, access its __class__
attribute in the call to the getfile()
method.
from another import Employee import inspect emp1 = Employee() # ๐๏ธ /home/borislav/Desktop/bobbyhadz_python/another.py print(inspect.getfile(emp1.__class__))
You can also use the type()
class to achieve the same result.
from another import Employee import inspect # ๐๏ธ /home/borislav/Desktop/bobbyhadz_python/another.py print(inspect.getfile(Employee)) emp1 = Employee() # ๐๏ธ /home/borislav/Desktop/bobbyhadz_python/another.py print(inspect.getfile(type(emp1)))
The type class returns the type of an object.
__class__
attribute on the object.The inspect.getfile()
method returns the absolute path to the module in which
the class is defined.
If you need to get a relative path, use the os.path.relpath()
method.
from another import Employee import inspect import os # ๐๏ธ another.py print(os.path.relpath(inspect.getfile(Employee)))
The os.path.relpath() method takes a file path and returns a relative version of the path.
Alternatively, you can use the os.path.abspath()
method.
This is a three-step process:
sys.modules
dictionary to get the module in which the class was
defined.__file__
attribute to get the pathname of the file from which the
module was loaded.os.path.abspath()
method to get a normalized, absolute version of
the path.import os import sys from another import Employee result = os.path.abspath(sys.modules[Employee.__module__].__file__) # ๐๏ธ /home/borislav/Desktop/bobbyhadz_python/another.py print(result) emp1 = Employee() result = os.path.abspath(sys.modules[type(emp1).__module__].__file__) print(result) # ๐๏ธ /home/borislav/Desktop/bobbyhadz_python/another.py
The os.path.abspath() method takes a path and returns a normalized, absolute version of the path.
Sys.modules is a dictionary that maps module names to modules that have already been loaded.
We used the __module__
attribute on the class to get the name of the module in
which the class was defined.
import os import sys from another import Employee print(Employee.__module__) # ๐๏ธ another # ๐๏ธ /home/borislav/Desktop/bobbyhadz_python/another.py print(sys.modules[Employee.__module__].__file__)
The __file__
attribute on the module returns the pathname of the file from
which the module was loaded.
Here is the complete code snippet.
import os import sys from another import Employee result = os.path.abspath(sys.modules[Employee.__module__].__file__) # ๐๏ธ /home/borislav/Desktop/bobbyhadz_python/another.py print(result) emp1 = Employee() result = os.path.abspath(sys.modules[type(emp1).__module__].__file__) print(result) # ๐๏ธ /home/borislav/Desktop/bobbyhadz_python/another.py
You can learn more about the related topics by checking out the following tutorials: