|
|
|
|
@ -6,146 +6,253 @@ from pathlib import Path
|
|
|
|
|
|
|
|
|
|
class FileManager:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
"""
|
|
|
|
|
初始化文件管理器
|
|
|
|
|
- 设置工作目录
|
|
|
|
|
- 初始化文件缓存
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现构造函数逻辑
|
|
|
|
|
|
|
|
|
|
# 实现构造函数逻辑123
|
|
|
|
|
# 1. 设置默认工作目录
|
|
|
|
|
self.working_directory = Path.cwd()
|
|
|
|
|
# 2. 初始化文件缓存
|
|
|
|
|
self.file_cache = {}
|
|
|
|
|
# 3. 创建必要的目录结构
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def list_files(self, directory: str, extensions: Optional[List[str]] = None) -> List[str]:
|
|
|
|
|
"""
|
|
|
|
|
列出目录中的文件
|
|
|
|
|
- 遍历指定目录
|
|
|
|
|
- 根据扩展名过滤文件(如果提供)
|
|
|
|
|
- 返回文件路径列表
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现文件列表逻辑
|
|
|
|
|
|
|
|
|
|
# 实现文件列表逻辑
|
|
|
|
|
# 1. 检查目录是否存在
|
|
|
|
|
if not os.path.exists(directory):
|
|
|
|
|
raise FileNotFoundError(f"目录 {directory} 不存在")
|
|
|
|
|
|
|
|
|
|
# 2. 遍历目录中的所有文件
|
|
|
|
|
# 3. 根据扩展名过滤文件(如果提供)
|
|
|
|
|
file_list = []
|
|
|
|
|
for root, _, files in os.walk(directory):
|
|
|
|
|
for file in files:
|
|
|
|
|
file_path = os.path.join(root, file)
|
|
|
|
|
# 3. 根据扩展名过滤文件(如果提供)
|
|
|
|
|
if extensions:
|
|
|
|
|
_, ext = os.path.splitext(file)
|
|
|
|
|
if ext.lower() in [e.lower() for e in extensions]:
|
|
|
|
|
file_list.append(file_path)
|
|
|
|
|
else:
|
|
|
|
|
file_list.append(file_path)
|
|
|
|
|
|
|
|
|
|
# 4. 返回文件路径列表
|
|
|
|
|
pass
|
|
|
|
|
return file_list
|
|
|
|
|
|
|
|
|
|
def copy_file(self, source: str, destination: str) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
复制文件
|
|
|
|
|
- 将文件从源路径复制到目标路径
|
|
|
|
|
- 返回操作结果
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现文件复制逻辑
|
|
|
|
|
|
|
|
|
|
# 实现文件复制逻辑
|
|
|
|
|
# 1. 检查源文件是否存在
|
|
|
|
|
# 2. 创建目标目录(如果不存在)
|
|
|
|
|
# 3. 执行文件复制操作
|
|
|
|
|
# 4. 处理异常情况
|
|
|
|
|
if not os.path.exists(source):
|
|
|
|
|
print(f"源文件 {source} 不存在")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# 2. 创建目标目录(如果不存在)
|
|
|
|
|
dest_dir = os.path.dirname(destination)
|
|
|
|
|
if dest_dir and not os.path.exists(dest_dir):
|
|
|
|
|
os.makedirs(dest_dir)
|
|
|
|
|
|
|
|
|
|
# 3. 执行文件复制操作
|
|
|
|
|
shutil.copy2(source, destination)
|
|
|
|
|
# 4. 处理异常情况
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"复制文件时出错: {e}")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# 5. 返回操作结果
|
|
|
|
|
pass
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def move_file(self, source: str, destination: str) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
移动文件
|
|
|
|
|
- 将文件从源路径移动到目标路径
|
|
|
|
|
- 返回操作结果
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现文件移动逻辑
|
|
|
|
|
|
|
|
|
|
# 实现文件移动逻辑
|
|
|
|
|
# 1. 检查源文件是否存在
|
|
|
|
|
# 2. 创建目标目录(如果不存在)
|
|
|
|
|
# 3. 执行文件移动操作
|
|
|
|
|
# 4. 处理异常情况
|
|
|
|
|
if not os.path.exists(source):
|
|
|
|
|
print(f"源文件 {source} 不存在")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# 2. 创建目标目录(如果不存在)
|
|
|
|
|
dest_dir = os.path.dirname(destination)
|
|
|
|
|
if dest_dir and not os.path.exists(dest_dir):
|
|
|
|
|
os.makedirs(dest_dir)
|
|
|
|
|
|
|
|
|
|
# 3. 执行文件移动操作
|
|
|
|
|
shutil.move(source, destination)
|
|
|
|
|
# 4. 处理异常情况
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"移动文件时出错: {e}")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# 5. 返回操作结果
|
|
|
|
|
pass
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def delete_file(self, file_path: str) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
删除文件
|
|
|
|
|
- 删除指定路径的文件
|
|
|
|
|
- 返回操作结果
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现文件删除逻辑
|
|
|
|
|
|
|
|
|
|
# 实现文件删除逻辑
|
|
|
|
|
# 1. 检查文件是否存在
|
|
|
|
|
# 2. 执行文件删除操作
|
|
|
|
|
# 3. 处理异常情况(如权限不足)
|
|
|
|
|
if not os.path.exists(file_path):
|
|
|
|
|
print(f"文件 {file_path} 不存在")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# 2. 执行文件删除操作
|
|
|
|
|
os.remove(file_path)
|
|
|
|
|
# 3. 处理异常情况(如权限不足)
|
|
|
|
|
except PermissionError:
|
|
|
|
|
print(f"没有权限删除文件 {file_path}")
|
|
|
|
|
return False
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"删除文件时出错: {e}")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# 4. 返回操作结果
|
|
|
|
|
pass
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def get_file_info(self, file_path: str) -> Optional[Dict[str, Any]]:
|
|
|
|
|
"""
|
|
|
|
|
获取文件信息
|
|
|
|
|
- 获取文件大小、修改时间等信息
|
|
|
|
|
- 返回信息字典
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现文件信息获取逻辑
|
|
|
|
|
|
|
|
|
|
# 实现文件信息获取逻辑
|
|
|
|
|
# 1. 检查文件是否存在
|
|
|
|
|
# 2. 获取文件基本信息(大小、修改时间等)
|
|
|
|
|
# 3. 获取文件扩展名和类型
|
|
|
|
|
# 4. 返回信息字典
|
|
|
|
|
pass
|
|
|
|
|
if not os.path.exists(file_path):
|
|
|
|
|
print(f"文件 {file_path} 不存在")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# 2. 获取文件基本信息(大小、修改时间等)
|
|
|
|
|
stat_info = os.stat(file_path)
|
|
|
|
|
file_size = stat_info.st_size
|
|
|
|
|
modification_time = stat_info.st_mtime
|
|
|
|
|
|
|
|
|
|
# 3. 获取文件扩展名和类型
|
|
|
|
|
_, ext = os.path.splitext(file_path)
|
|
|
|
|
|
|
|
|
|
# 4. 返回信息字典
|
|
|
|
|
file_info = {
|
|
|
|
|
"path": file_path,
|
|
|
|
|
"size": file_size,
|
|
|
|
|
"modification_time": modification_time,
|
|
|
|
|
"extension": ext.lower(),
|
|
|
|
|
"name": os.path.basename(file_path)
|
|
|
|
|
}
|
|
|
|
|
return file_info
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"获取文件信息时出错: {e}")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
class DocumentOrganizer:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
"""
|
|
|
|
|
初始化文档整理器
|
|
|
|
|
- 设置分类规则
|
|
|
|
|
- 初始化标签系统
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现构造函数逻辑
|
|
|
|
|
|
|
|
|
|
# 实现构造函数逻辑
|
|
|
|
|
# 1. 设置默认分类规则
|
|
|
|
|
self.categorization_rules = {
|
|
|
|
|
"images": [".jpg", ".jpeg", ".png", ".gif", ".bmp"],
|
|
|
|
|
"documents": [".pdf", ".doc", ".docx", ".txt", ".md"],
|
|
|
|
|
"videos": [".mp4", ".avi", ".mkv", ".mov"],
|
|
|
|
|
"audio": [".mp3", ".wav", ".flac"],
|
|
|
|
|
"archives": [".zip", ".rar", ".7z", ".tar"]
|
|
|
|
|
}
|
|
|
|
|
# 2. 初始化标签系统
|
|
|
|
|
self.tags = {}
|
|
|
|
|
# 3. 创建必要的目录结构
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def categorize_documents(self, directory: str) -> Dict[str, List[str]]:
|
|
|
|
|
"""
|
|
|
|
|
分类文档
|
|
|
|
|
- 根据预设规则对文档进行分类
|
|
|
|
|
- 返回分类结果字典
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现文档分类逻辑
|
|
|
|
|
|
|
|
|
|
# 1. 遍历目录中的所有文件
|
|
|
|
|
if not os.path.exists(directory):
|
|
|
|
|
raise FileNotFoundError(f"目录 {directory} 不存在")
|
|
|
|
|
|
|
|
|
|
# 2. 根据文件类型或内容特征进行分类
|
|
|
|
|
categorized_files = {category: [] for category in self.categorization_rules}
|
|
|
|
|
uncategorized = []
|
|
|
|
|
|
|
|
|
|
for root, _, files in os.walk(directory):
|
|
|
|
|
for file in files:
|
|
|
|
|
file_path = os.path.join(root, file)
|
|
|
|
|
_, ext = os.path.splitext(file)
|
|
|
|
|
|
|
|
|
|
# 根据扩展名分类
|
|
|
|
|
categorized = False
|
|
|
|
|
for category, extensions in self.categorization_rules.items():
|
|
|
|
|
if ext.lower() in extensions:
|
|
|
|
|
categorized_files[category].append(file_path)
|
|
|
|
|
categorized = True
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
if not categorized:
|
|
|
|
|
uncategorized.append(file_path)
|
|
|
|
|
|
|
|
|
|
categorized_files["uncategorized"] = uncategorized
|
|
|
|
|
|
|
|
|
|
# 3. 返回分类结果字典 {类别: [文件列表]}
|
|
|
|
|
pass
|
|
|
|
|
return categorized_files
|
|
|
|
|
|
|
|
|
|
def add_tag_to_file(self, file_path: str, tag: str) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
为文件添加标签
|
|
|
|
|
- 在文件元数据中添加标签信息
|
|
|
|
|
- 返回操作结果
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现标签添加逻辑
|
|
|
|
|
|
|
|
|
|
# 实现标签添加逻辑
|
|
|
|
|
# 1. 检查文件是否存在
|
|
|
|
|
if not os.path.exists(file_path):
|
|
|
|
|
print(f"文件 {file_path} 不存在")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# 2. 读取文件元数据
|
|
|
|
|
# 3. 添加新标签
|
|
|
|
|
if file_path not in self.tags:
|
|
|
|
|
self.tags[file_path] = []
|
|
|
|
|
|
|
|
|
|
if tag not in self.tags[file_path]:
|
|
|
|
|
self.tags[file_path].append(tag)
|
|
|
|
|
|
|
|
|
|
# 4. 保存更新后的元数据
|
|
|
|
|
# 5. 返回操作结果
|
|
|
|
|
pass
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def search_files_by_tag(self, tag: str) -> List[str]:
|
|
|
|
|
"""
|
|
|
|
|
根据标签搜索文件
|
|
|
|
|
- 查找具有指定标签的所有文件
|
|
|
|
|
- 返回文件路径列表
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现标签搜索逻辑
|
|
|
|
|
|
|
|
|
|
# 实现标签搜索逻辑
|
|
|
|
|
# 1. 遍历文件数据库或目录
|
|
|
|
|
# 2. 查找包含指定标签的文件
|
|
|
|
|
matching_files = []
|
|
|
|
|
for file_path, tags in self.tags.items():
|
|
|
|
|
if tag in tags:
|
|
|
|
|
matching_files.append(file_path)
|
|
|
|
|
|
|
|
|
|
# 3. 返回文件路径列表
|
|
|
|
|
pass
|
|
|
|
|
return matching_files
|
|
|
|
|
|
|
|
|
|
def backup_documents(self, source_dir: str, backup_dir: str) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
备份文档
|
|
|
|
|
- 将源目录中的文档备份到备份目录
|
|
|
|
|
- 返回操作结果
|
|
|
|
|
"""
|
|
|
|
|
# TODO: 实现文档备份逻辑
|
|
|
|
|
|
|
|
|
|
# 实现文档备份逻辑
|
|
|
|
|
# 1. 创建备份目录(如果不存在)
|
|
|
|
|
if not os.path.exists(backup_dir):
|
|
|
|
|
os.makedirs(backup_dir)
|
|
|
|
|
|
|
|
|
|
# 2. 遍历源目录中的所有文件
|
|
|
|
|
# 3. 复制文件到备份目录
|
|
|
|
|
# 4. 处理异常情况
|
|
|
|
|
# 5. 返回操作结果
|
|
|
|
|
pass
|
|
|
|
|
if not os.path.exists(source_dir):
|
|
|
|
|
print(f"源目录 {source_dir} 不存在")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# 使用shutil.copytree进行目录复制
|
|
|
|
|
# 如果备份目录已存在且不为空,需要先清空或使用其他方法
|
|
|
|
|
for root, dirs, files in os.walk(source_dir):
|
|
|
|
|
# 计算相对路径
|
|
|
|
|
rel_path = os.path.relpath(root, source_dir)
|
|
|
|
|
dest_path = os.path.join(backup_dir, rel_path) if rel_path != '.' else backup_dir
|
|
|
|
|
|
|
|
|
|
# 创建目标目录
|
|
|
|
|
if not os.path.exists(dest_path):
|
|
|
|
|
os.makedirs(dest_path)
|
|
|
|
|
|
|
|
|
|
# 复制文件
|
|
|
|
|
for file in files:
|
|
|
|
|
src_file = os.path.join(root, file)
|
|
|
|
|
dest_file = os.path.join(dest_path, file)
|
|
|
|
|
shutil.copy2(src_file, dest_file)
|
|
|
|
|
|
|
|
|
|
# 3. 处理异常情况
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"备份文档时出错: {e}")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# 4. 返回操作结果
|
|
|
|
|
return True
|