2
\$\begingroup\$

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. :}

\$\endgroup\$
4
  • \$\begingroup\$ What is the point of the variable i? \$\endgroup\$ – 200_success Apr 11 '18 at 5:12
  • \$\begingroup\$ I was thinking of creating a loop that have true while i > 0, where while True, to change to while i > 0:, but didn't knew if that's a good idea, so as for now I lefted it, but if it's not needed then I think I will remove later on :) \$\endgroup\$ – simkusr Apr 11 '18 at 5:16
  • \$\begingroup\$ I've thought about this multiple times now and still don't get what you're trying to do. What do you mean by 'I can construct the path to the ./rootProjectDir/sub/subsub/subsubfile.txt to get that subsubfile.txt.'? What problem are you currently experiencing that GetRootPath is supposed to solve? \$\endgroup\$ – Daniel Apr 11 '18 at 15:11
  • \$\begingroup\$ Imagine my structure above and that you are working within .py file. Now you need to get that .txt file content from the structure above. How to do that? 1st you can hard-code whole path to the .txt file and that would be fine, but imagine if you have 10 or more .py files in many different structures and differently nested .py files. Now hard-coding whole path in these scattered .py files to get that .txt file looks to much of work. So I decided to create relative path to my .txt file that I could get it by just providing the path from root to .txt file. \$\endgroup\$ – simkusr Apr 12 '18 at 3:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.