Skip to main content
deleted 2 characters in body
Source Link
toolic
  • 16.4k
  • 6
  • 29
  • 221

I have written a class that locates the relative root project directory path from where I'm calling that class.

E.g., I have this 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 foundfind any better solution... I I hope there is, but I wrote this 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? Or is there a better Pythonic way of navigating your project folder to get files and, etc., as you need? Because my My GetRootPath class looks kind of Nonlike a 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. :}

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

I have written a class that locates the relative root project directory path from where I'm calling that class.

E.g., I have this 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 find any better solution. I hope there is, but I wrote this 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, etc., as you need? My GetRootPath class looks like a non-Pythonic way of doing this.

I'm working in PyCharm, and my project is created with it, so it's not just folders, I guess.

deleted 18 characters in body; edited tags; edited title
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Need advice Class to construct paths based on my relativethe path to the current class creationfile

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

So my question, isIs 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 PythonicPythonic 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. :}

Need advice on my relative path class creation

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

So my question, 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. :}

Class to construct paths based on the path to the current class file

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

Source Link
simkusr
  • 215
  • 1
  • 2
  • 9

Need advice on my relative path class creation

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

So my question, 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. :}