以下是为您提供的资源管理器的代码示例,使用 Python 实现: ```python import os import shutil class ResourceManager: def __init__(self): """ 初始化资源管理器 """ self.current_path = os.getcwd() # 获取当前工作目录 def list_files_and_folders(self): """ 列出当前目录下的文件和文件夹 """ contents = os.listdir(self.current_path) for item in contents: print(item) def create_folder(self, folder_name): """ 创建新文件夹 参数: folder_name (str): 要创建的文件夹名称 """ try: os.mkdir(os.path.join(self.current_path, folder_name)) print(f"创建文件夹 {folder_name} 成功") except FileExistsError: print(f"文件夹 {folder_name} 已存在") def create_file(self, file_name): """ 创建新文件 参数: file_name (str): 要创建的文件名称 """ try: with open(os.path.join(self.current_path, file_name), 'w') as f: pass print(f"创建文件 {file_name} 成功") except FileExistsError: print(f"文件 {file_name} 已存在") def move_item(self, source_path, destination_path): """ 移动文件或文件夹 参数: source_path (str): 源文件或文件夹路径 destination_path (str): 目标路径 """ try: shutil.move(source_path, destination_path) print(f"移动 {source_path} 到 {destination_path} 成功") except FileNotFoundError: print(f"源文件或文件夹 {source_path} 未找到") except shutil.Error: print("移动操作失败") def copy_item(self, source_path, destination_path): """ 复制文件或文件夹 参数: source_path (str): 源文件或文件夹路径 destination_path (str): 目标路径 """ try: if os.path.isdir(source_path): shutil.copytree(source_path, destination_path) else: shutil.copy(source_path, destination_path) print(f"复制 {source_path} 到 {destination_path} 成功") except FileNotFoundError: print(f"源文件或文件夹 {source_path} 未找到") except shutil.Error: print("复制操作失败") def delete_item(self, item_path): """ 删除文件或文件夹 参数: item_path (str): 要删除的文件或文件夹路径 """ try: if os.path.isdir(item_path): shutil.rmtree(item_path) else: os.remove(item_path) print(f"删除 {item_path} 成功") except FileNotFoundError: print(f"文件或文件夹 {item_path} 未找到") except OSError: print("删除操作失败") if __name__ == "__main__": manager = ResourceManager() # 示例调用 manager.list_files_and_folders() manager.create_folder("new_folder") manager.create_file("new_file.txt") manager.move_item("new_file.txt", "new_folder") manager.copy_item("new_folder", "copy_of_new_folder") manager.delete_item("new_folder") ``` 额外需要注意的逻辑点: 1. 处理文件或文件夹不存在的情况时,要给出清晰明确的错误提示。 2. 对于复制和移动操作,如果目标路径已存在同名文件或文件夹,需要考虑如何处理冲突。 3. 注意权限问题,确保程序有足够的权限进行文件和文件夹的操作。 [2024-12-05 15:03:36 | AI写代码神器 | 883点数解答]