SanYeCao-Nonebot/src/clover_image/delete_file.py
SlyAimer feb53b1a31 feat(jm): 添加JM漫画下载功能及相关模块
新增jm_download插件、jm_comic核心下载逻辑、disguise_pdf文件处理工具
扩展delete_file功能,添加批量删除和文件夹删除方法
更新路径配置和菜单选项
2025-03-31 17:59:30 +08:00

34 lines
No EOL
966 B
Python

import os
import asyncio
import shutil
async def delete_file(file_path):
try:
os.remove(file_path)
except FileNotFoundError:
print(f"文件 {file_path} 不存在。")
except Exception as e:
print(f"删除文件时发生错误: {e}")
async def delete_file_batch(file_paths):
"""
批量删除文件的异步函数(并行版本)
:param file_paths: 需要删除的文件路径列表
"""
tasks = [delete_file(path) for path in file_paths]
results = await asyncio.gather(*tasks, return_exceptions=True)
for path, result in zip(file_paths, results):
if isinstance(result, Exception):
print(f"删除 {path} 失败: {result}")
async def delete_folder(folder_path):
try:
shutil.rmtree(folder_path)
except FileNotFoundError:
print(f"文件夹 {folder_path} 不存在。")
except Exception as e:
print(f"删除文件夹时发生错误: {e}")