W
W
w0lkolak2021-10-22 16:02:44
Python
w0lkolak, 2021-10-22 16:02:44

How to open file from child class directory by parent class method?

There is a parent class machine in the machine folder. There is a child class machine5 in the machine 5 folder. I know how to get the directory of the current script via os.getcwd() and os.path.dirname, but this gives the parent class folder because the method is physically described in it. Can I somehow refer to the folder of the child class that this method calls? Should I send the address directly to the method?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-10-22
@w0lkolak

You can use this class method:

import sys

class Machine:
   
    @classmethod
    def get_file_v1(cls) -> str:
        return sys.modules[cls.__module__].__file__

PS: Quite an interesting problem. I will need it myself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question