aicheckv2-api/utils/os_utils.py

113 lines
3.2 KiB
Python
Raw Normal View History

2025-04-11 08:54:28 +08:00
import os
import shutil
from fastapi import UploadFile
from PIL import Image
def file_path(*path):
"""
拼接返回文件路径
:param path:
:return:
"""
return_path = os.path.join(*path)
return return_path
def create_folder(*path):
"""根据路径创建文件夹"""
folder_path = os.path.join(*path)
try:
os.makedirs(folder_path, exist_ok=True)
except Exception as e:
print(f"创建文件夹时错误: {e}")
return folder_path
def save_images(*path, file: UploadFile):
"""
保存上传的图片
:param path: 路径
:param file: 文件
:return:
"""
save_path = os.path.join(*path, file.filename)
os.makedirs(os.path.dirname(save_path), exist_ok=True)
with open(save_path, "wb") as f:
for line in file.file:
f.write(line)
return save_path
def create_thumbnail(input_image_path, out_image_path, size=(116, 70)):
"""
给图片生成缩略图
:param input_image_path:
:param out_image_path:
:param size: 缩略的尺寸
:return:
"""
with Image.open(input_image_path) as image:
# 使用thumbnail方法生成缩略图参数size指定缩略图的最大尺寸
# 注意thumbnail方法会保持图片的宽高比不变
image.thumbnail(size)
os.makedirs(os.path.dirname(out_image_path), exist_ok=True)
# 保存生成的缩略图
image.save(out_image_path, 'JPEG')
def copy_and_rename_file(src_file_path, dst_dir, new_name):
"""
复制文件到指定目录并重命名保持文件的后缀不变
:param src_file_path: 源文件路径
:param dst_dir: 目标目录
:param new_name: 新文件名不含扩展名
"""
# 获取文件的完整文件名(包括扩展名)
base_name = os.path.basename(src_file_path)
# 分离文件名和扩展名
file_name, file_extension = os.path.splitext(base_name)
# 构建新的文件名和目标路径
new_file_name = f"{new_name}{file_extension}"
dst_file_path = os.path.join(dst_dir, new_file_name)
# 确保目标目录存在
os.makedirs(dst_dir, exist_ok=True)
# 复制文件到目标位置并重命名
shutil.copy(src_file_path, dst_file_path)
def delete_file_if_exists(*file_paths: str):
"""
删除文件
:param file_path:
:return:
"""
for path in file_paths:
if os.path.exists(path): # 检查文件是否存在
os.remove(path) # 删除文件
def delete_paths(paths):
"""
删除给定路径数组中的每个路径及其包含的所有内容
:param paths: 文件或目录路径的列表
"""
for path in paths:
if os.path.exists(path):
try:
if os.path.isfile(path) or os.path.islink(path):
# 如果是文件或符号链接,则删除
os.remove(path)
print(f"Deleted file: {path}")
elif os.path.isdir(path):
# 如果是目录,则递归删除
shutil.rmtree(path)
except Exception as e:
print(f"路径删除失败 {path}: {e}")
else:
print(f"路径不存在: {path}")