[Python] 纯文本查看 复制代码 import os
import logging
# 配置日志记录
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def delete_file(file_path):
"""
删除指定路径的文件,如果文件不存在或删除失败,记录相应的日志信息。
:param file_path: 要删除的文件路径
"""
if os.path.exists(file_path):
try:
os.remove(file_path)
logging.info(f"文件 {file_path} 已成功删除。")
except FileNotFoundError:
logging.error(f"文件 {file_path} 不存在。")
except PermissionError:
logging.error(f"没有权限删除文件 {file_path}。")
except Exception as e:
logging.error(f"删除文件 {file_path} 时发生未知错误: {e}")
else:
logging.warning(f"文件 {file_path} 不存在。")
# 使用示例
font_file_path = 'font_file.ttf'
delete_file(font_file_path) |