mn27
mn27

Reputation: 131

Implement FileSystem

I had a company assign me an assignment to implement a fileSystem class to run shell commands through python without using any libraries. Does anyone have any suggestions on how to get started? Not quite sure how to tackle this problem.

Problem:

Implement a FileSystem class using python

Root path is '/'.
Path separator is '/'.
Parent directory is addressable as '..'.
Directory names consist only of English alphabet letters (A-Z and a-z).
All functions should support both relative and absolute paths. All function parameters are the minimum required/recommended parameters. Any additional class/function can be added.

What I've worked on so far:

class Path:

    def __init__(self, path):
        self.current_path = path.split("/")
    
    def cd(self, new_path):
        new_split = new_path.split("/")
        for i in new_split:
            if i == "..":
                new_split.pop(0)
                self.current_path = self.current_path[:-1]
        self.current_path += new_split

    def getString(self):
        return "/".join(self.current_path)

    def pwd(self, path):
        return self.current_path

    def mkdir(): 
        pass

    def rmdir():
        pass

#driver code 

fs = Path()
fs.mkdir('usr')
fs.cd('usr')
fs.mkdir('local')
fs.cd('local')
return fs.pwd()

Upvotes: 3

Views: 433

Answers (1)

mn27
mn27

Reputation: 131

So, this is what I came up with. I know I need to clean it up

'''

class Path:

    dir_stack = []

    def __init__(self):
        print("started")
        main_dir = {'/': {}}
        self.dir_stack.insert( len(self.dir_stack), main_dir)

    def getCurrentMap():
        global current_Level
        current_Level = self.dir_stack[len(self.dir_stack) - 1]

   
    def cd(self, folder):
        if(folder == '../'):
         self.dir_stack.pop()

        current_Level = self.dir_stack[len(self.dir_stack) - 1]
        current_Map = current_Level[(list(current_Level.keys())[0])]
        print('lev', current_Map)
        if folder in current_Map:
            print('here')
            self.dir_stack.insert(len(self.dir_stack), current_Map)
        else:
            print ("no existing folder")

    def pwd(self):
        path = ''
        print(self.dir_stack)
        for x in self.dir_stack:
            path += (list(x.keys())[0]) + '/'
        print(path)

    def ls(self):
        current_Level = self.dir_stack[len(self.dir_stack) - 1]
        current_Map = current_Level[(list(current_Level.keys())[0])]
        print(current_Map)


    def mkdir(self, folder_Name):
        current_Level = self.dir_stack[len(self.dir_stack) - 1]
        newDir = {folder_Name: {}}
        current_Map = current_Level[(list(current_Level.keys())[0])]
       
        if folder_Name in current_Map:
         warning = folder_Name +  ' already exists in directory'
         print(warning)
        else:
         current_Map.update(newDir)
 
    def rmdir(self, folder_Name):
        current_Level = self.dir_stack[len(self.dir_stack) - 1]
        #make global var current_Map
        current_Map = current_Level[(list(current_Level.keys())[0])]  
        if folder_Name in current_Map:
          del current_Map[folder_Name]
        else:
            print('folder doesnt exist')


# driver code


fs = Path()
fs.mkdir('usr')
fs.mkdir('new')
fs.mkdir('files')
fs.cd('usr')
fs.mkdir('local')
fs.cd('new')
fs.pwd()
fs.cd('../')
fs.ls()
# fs.mkdir('local')
# fs.cd('local')
fs.pwd()

Upvotes: 1

Related Questions