Filesystems 实现文件系统

Filesystems 实现文件系统,filesystems,Filesystems,我让一家公司给我分配了一个任务来实现一个文件系统类,以便在不使用任何库的情况下通过python运行shell命令。有人对如何开始有什么建议吗?不太清楚如何解决这个问题 问题: 使用python实现文件系统类 根路径为“/”。 路径分隔符为“/”。 父目录可寻址为“..”。 目录名仅由英文字母(A-Z和A-Z)组成。 所有函数都应支持相对路径和绝对路径。 所有功能参数均为最低要求/建议参数。 可以添加任何其他类/函数 到目前为止我所做的工作: class Path: def __init

我让一家公司给我分配了一个任务来实现一个文件系统类,以便在不使用任何库的情况下通过python运行shell命令。有人对如何开始有什么建议吗?不太清楚如何解决这个问题

问题:

使用python实现文件系统类

根路径为“/”。
路径分隔符为“/”。
父目录可寻址为“..”。
目录名仅由英文字母(A-Z和A-Z)组成。
所有函数都应支持相对路径和绝对路径。 所有功能参数均为最低要求/建议参数。 可以添加任何其他类/函数

到目前为止我所做的工作:

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()

这就是我想到的。我知道我需要把它清理干净

'''

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()