1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
| BANNER = """ ============================================================ 欢迎来到模拟文件系统! ============================================================ 功能如下: 查看当前目录: ls 改变目录格式: cd <目录名> 创建文件格式: touch <文件名> 创建目录格式: mkdir <目录名> 写入文件格式: write <文件名> <内容> 读取文件格式: read <文件名> 删除文件格式: rmfile <文件名> 删除目录格式: rmdir <目录名> 查看当前路径格式: pwd 重命名文件格式: rename <文件名> 退出文件系统: exit system ------------------------------------------------------------ """
class Output: def __init__(self, output): pass
@staticmethod def error(output): print(f"\033[31m{output}\033[0m")
@staticmethod def info(output): print(f"\033[36m{output}\033[0m")
@staticmethod def hint(output): print(f"\033[34m{output}\033[0m")
@staticmethod def warn(output): print(f"\033[33m{output}\033[0m")
@staticmethod def func(output): print(f"\033[35m{output}\033[0m")
class File: def __init__(self, name, content=""): self.name = name self.content = content
def write(self, content): self.content = content
def read(self): return self.content
class Directory: def __init__(self, name, parent): self.name = name self.parent = parent self.subdirs = {} self.files = {}
def create_file(self, name, content=""): if name not in self.files: self.files[name] = File(name, content) Output.info(f"文件“{name}”创建成功!") else: Output.error(f"错误:文件“{name}”已经存在!")
def create_subdir(self, name, parent): if name not in self.subdirs: self.subdirs[name] = Directory(name, parent) Output.info(f"文件夹“{name}”创建成功!") else: Output.error(f"错误:文件夹“{name}”已经存在!")
def delete_file(self, name): if name in self.files: self.files.pop(name) Output.info(f"文件“{name}”删除成功!") else: Output.error(f"错误:文件“{name}”不存在!")
def delete_subdir(self, name): if name in self.subdirs: self.subdirs.pop(name) Output.info(f"文件夹“{name}”删除成功!") else: Output.error(f"错误:文件夹“{name}”不存在!")
def list_contents(self): Output.warn(f"文件夹“{self.name}”所包含内容如下:") for subdir in self.subdirs: Output.info(f"[DIR] {subdir}") for file in self.files: Output.info(f"[FILE] {file}")
def rename_file(self, old_name, new_name): if old_name in self.files: self.files[new_name] = self.files.pop(old_name) self.files[new_name].name = new_name Output.info(f"文件名修改成功!{old_name} -> {new_name}") else: Output.error(f"错误:文件“{old_name}”不存在!")
def write_file(self, name, content): if name not in self.files: self.files[name] = File(name, content) Output.warn(f"文件“{name}”创建成功,并且已写入文件!") else: self.files[name].write(content) Output.info(f"文件“{name}”写入成功!")
def read_file(self, name): if name not in self.files: Output.error(f"错误:文件“{name}”不存在!") elif self.files[name].content == "": Output.warn(f"提示:文件“{name}”为空!") else: Output.warn(f"文件“{name}”内容如下:") Output.info(self.files[name].read())
class FileSystem: def __init__(self): self.root = Directory("~", parent=None) self.current = self.root
def cd(self, name): if name == "..": if not self.current.parent: Output.error(f"错误:当前已在根目录,无法向上切换目录!") else: self.current = self.current.parent Output.info(f"目录切换成功,当前目录为:{self.current.name}") elif name in self.current.subdirs: self.current = self.current.subdirs[name] Output.warn(f"目录切换成功,当前目录为:{self.current.name}") else: Output.error(f"错误:未找到目录“{name}”,请检查目录名是否正确!")
def ls(self, name): if not name: self.current.list_contents() else: Output.warn(f"错误:命令错误,请重试mkdi!")
def touch(self, name): self.current.create_file(name)
def mkdir(self, name): self.current.create_subdir(name, self.current)
def write(self, name, content): self.current.write_file(name, content)
def read(self, name): self.current.read_file(name)
def pwd(self): current = self.current path_parts = []
while current is not None: path_parts.insert(0, current.name) current = getattr(current, "parent", None)
pwd_path = "/".join(path_parts) Output.warn(f"当前路径为:/{pwd_path}")
def rmfile(self, name): self.current.delete_file(name)
def rmdir(self, name): self.current.delete_subdir(name)
def rename(self, old_name, new_name): self.current.rename_file(old_name, new_name)
def main(): Output.func(BANNER)
fs = FileSystem() while True: user_input = input("\033[34m请输入文件操作命令:\033[0m") if user_input == "exit system": Output.error("再见!退出模拟文件系统。") break
try: if user_input.startswith("write") or user_input.startswith("rename"): parts = user_input.split(" ", 2) if len(parts) == 3: user_order, user_param, user_content = parts else: Output.error("错误:命令参数不足!") continue elif " " in user_input: user_order, user_param = user_input.split(" ", 1) else: user_order = user_input.strip() user_param = "" if user_order == "ls": fs.ls(user_param) elif user_order == "cd": fs.cd(user_param) elif user_order == "mkdir": fs.mkdir(user_param) elif user_order == "touch": fs.touch(user_param) elif user_order == "write": fs.write(user_param, user_content) elif user_order == "read": fs.read(user_param) elif user_order == "pwd": fs.pwd() elif user_order == "rmfile": fs.rmfile(user_param) elif user_order == "rmdir": fs.rmdir(user_param) elif user_order == "rename": fs.rename(user_param, user_content) else: Output.error("错误:无效的命令或参数,请重试!") except ValueError as ve: Output.error(f"命令解析错误:{ve}") except Exception as e: Output.errorsl(f"系统错误:{e}")
if __name__ == '__main__': main()
|