I'm trying to find a way to represent a file structure as python objects so I can easily get a specific path without having to type out the string everything. This works for my case because I have a static file structure (Not changing).
I thought I could represent directories as class's and files in the directory as class/static variables.
I want to be able to navigate through python objects so that it returns the path I want i.e:
print(FileStructure.details.file1) # root\details\file1.txt
print(FileStructure.details) # root\details
What I get instead from the code below is:
print("{0}".format(FileStructure())) # root
print("{0}".format(FileStructure)) # <class '__main__.FileStructure'>
print("{0}".format(FileStructure.details)) # <class '__main__.FileStructure.details'>
print("{0}".format(FileStructure.details.file1)) # details\file1.txt
The code I have so far is...
import os
class FileStructure(object): # Root directory
root = "root"
class details(object): # details directory
root = "details"
file1 = os.path.join(root, "file1.txt") # File in details directory
file2 = os.path.join(root, "file2.txt") # File in details directory
def __str__(self):
return f"{self.root}"
def __str__(self):
return f"{self.root}"
I don't want to have to instantiate the class to have this work. My question is:
- How can I call the class object and have it return a string instead of the < class ....> text
- How can I have nested classes use their parent classes?