我正在尝试在python中创建类似FileSystem的东西。我的目标是制作可以包含一些数据的文件和可以包含一些文件的目录。然后我有一些简单的命令
cd
LS
mk_dir
mk_file
所以,现在我遇到了这个问题: DirectoryBase类与Directory不同,因为它没有ParentDir(其中的目录是文件/目录保存)。然后有扩展DirectoryBase的Directory类并覆盖path()方法(返回文件/ dir路径的方法)和数据dict有另一个默认密钥:' ..',但是当我尝试要在数据中找到它,它会引发KeyError。另一个奇怪的事情是,我正在测试文件/目录是否存在,如果不存在,它将返回,所以我不知道这是怎么可能的,它得到了KeyError。
这是我的代码:
class AttributeDictionary(dict):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.__getattr__ = self.__getitem__
self.__setattr__ = self.__setitem__
self.__delattr__ = self.__delitem__
class FileManager(object):
def __init__(self):
self.disk_dir = DirectoryBase('__disk__')
self.current_dir = self.disk_dir
def _find(self, path):
""" Gets file/dir object using path """
_cd = self.current_dir
for part in path:
_cd = _cd[part]
return _cd
def cmd_cd(self, path):
""" Classic unix `cd` command """
f = self._find(path)
if f is DirectoryBase:
self.current_dir = f
else:
return FileSystemError('DirectoryNotFound')
def cmd_ls(self):
""" Classic unix `ls` command """
return ConsoleOutput(''.join([f'{i}\n' for i in self.current_dir.data]))
def cmd_mkfile(self, name):
""" file making command """
self.current_dir.__setitem__(name, File(name, self.current_dir))
def cmd_mkdir(self, name):
""" file making command """
self.current_dir.__setitem__(name, Directory(name, self.current_dir))
class File(object):
def __init__(self, name, pd):
self.data = [0x00]
self.name = name
self.pd = pd
def path(self):
return [self.name] + self.pd.path()
def __sizeof__(self):
return len(self.data)
class DirectoryBase(AttributeDictionary):
def __init__(self, name, **kwargs):
super().__init__(**kwargs)
self.name = name
self.data = {'.': self}
def path(self):
return self.name
def __getitem__(self, item):
if item in self.data.keys():
return FileSystemError('FileNotFound')
return self.data[item] # KeyError: '..' found here
def __setitem__(self, key, value):
if key in self.data.keys():
return FileSystemError('FileAlreadyExists')
self.data[key] = value
def __delitem__(self, key):
if key in self.data.keys():
return FileSystemError('FileNotFound')
self.data.__delitem__(key)
class Directory(DirectoryBase):
def __init__(self, name, pd, **kwargs):
super().__init__(name, **kwargs)
self.pd = pd
self.data = {'.': self, '..': self.pd}
def path(self):
return [self.name] + self.pd.path()
if __name__ == '__main__':
fs = FileManager()
fs.cmd_mkdir('test')
print(fs.cmd_ls())
fs.cmd_cd(['test'])
fs.cmd_mkfile('idk.txt')
print(fs.cmd_ls())
fs.cmd_cd(['..'])
print(fs.cmd_ls())
错误如下所示:
.
test
Traceback (most recent call last):
.
File "/Users/jan/PycharmProjects/Xaon/src/fs.py", line 106, in <module>
test
fs.cmd_cd(['..'])
idk.txt
File "/Users/jan/PycharmProjects/Xaon/src/fs.py", line 32, in cmd_cd
f = self._find(path)
File "/Users/jan/PycharmProjects/Xaon/src/fs.py", line 27, in _find
_cd = _cd[part]
File "/Users/jan/PycharmProjects/Xaon/src/fs.py", line 76, in __getitem__
return self.data[item]
KeyError: '..'
或删除打印时:
Traceback (most recent call last):
File "/Users/jan/PycharmProjects/Xaon/src/fs.py", line 106, in <module>
fs.cmd_cd(['..'])
File "/Users/jan/PycharmProjects/Xaon/src/fs.py", line 32, in cmd_cd
f = self._find(path)
File "/Users/jan/PycharmProjects/Xaon/src/fs.py", line 27, in _find
_cd = _cd[part]
File "/Users/jan/PycharmProjects/Xaon/src/fs.py", line 76, in __getitem__
return self.data[item]
KeyError: '..'
抱歉,如果它太混乱,我是初学者,我的英语也很糟糕。
答案 0 :(得分:2)
代码逻辑存在缺陷:
def __getitem__(self, item):
if item in self.data.keys():
return FileSystemError('FileNotFound')
return self.data[item] # KeyError: '..' found here
当密钥存在时,您将抛出错误。只是否定条件(并删除.keys()
无效和unpythonic,甚至是Python 2中的性能杀手):
def __getitem__(self, item):
if item not in self.data:
return FileSystemError('FileNotFound')
return self.data[item]
__delitem__
BTW中存在相同的错误。
修复后(并使用str
将大多数未显示/未显示的类存根)我得到以下输出:
.
test
.
idk.txt
test
.
idk.txt
test