I have written a class that locates relative root project directory path from where I'm calling that class.
E.g. I have structure:
rootProjectDir |_sub |__subsub |___subsubfile.txt |_another_sub |__another_sub_file.py
and what I'm doing is when I'm working within
another_sub_file.py I'm calling my class
GetRootPath to get
/home/user/dev/rootProjectDir and then I can construct the path to the
./rootProjectDir/sub/subsub/subsubfile.txt to get that
subsubfile.txt. I didn't found any better solution... I hope there is, but I wrote class helper for me to achieve this:
import os class GetRootPath: def __init__(self): self.rootFolder = '/rootProjectDir' def get_root(self): working_file_dir = os.path.dirname(os.path.abspath(__file__)) i = len(working_file_dir.split('/')) root = list(working_file_dir.split('/')) new_root =  for item in root[1:]: new_root.append('/' + str(item)) while True: if self.rootFolder == new_root[-1]: builded = ''.join(new_root) + '/' return builded else: new_root.pop() i -= 1
Is there any better way to achieve my goal and also, what could be done better with my class to improve performance and stability of creating relative path to root directory of my project. Or is there a better Pythonic way of navigating your project folder to get files and etc as you need? Because my
GetRootPath class looks kind of Non-Pythonic way of doing this.
p.s. I'm working in PyCharm and my project is created with it, so it's not just folders... I guess. :}