You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.6 KiB
Python
64 lines
1.6 KiB
Python
import json
|
|
import os
|
|
import shutil
|
|
from loguru import logger
|
|
|
|
|
|
# 将字典数据保存为 JSON 文件
|
|
def save_json(data: list, file_path: str):
|
|
with open(file_path, "w") as f:
|
|
json.dump(data, f)
|
|
|
|
|
|
def load_json(file_path: str):
|
|
with open(file_path, "r") as f:
|
|
return json.load(f)
|
|
|
|
|
|
def is_dir_empty(path: str) -> bool:
|
|
return len(os.listdir(path)) == 0
|
|
|
|
|
|
def remove_files_in_dir(folder_path: str):
|
|
for filename in os.listdir(folder_path):
|
|
file_path = os.path.join(folder_path, filename)
|
|
try:
|
|
if os.path.isfile(file_path) or os.path.islink(file_path):
|
|
os.unlink(file_path) # 删除文件
|
|
elif os.path.isdir(file_path):
|
|
shutil.rmtree(file_path) # 递归删除子文件夹及其中文件
|
|
except Exception as e:
|
|
print(f"Failed to delete {file_path}. Reason: {e}")
|
|
|
|
|
|
def get_files_in_dir(dir):
|
|
file_list = []
|
|
for root, dirs, files in os.walk(dir):
|
|
for file in files:
|
|
file_list.append(os.path.join(root, file))
|
|
return file_list
|
|
|
|
|
|
def create_dir(dir):
|
|
if not os.path.exists(dir):
|
|
os.makedirs(dir)
|
|
logger.debug(f'文件夹创建成功:{dir}')
|
|
else:
|
|
logger.debug(f'文件夹已经存在:{dir}')
|
|
|
|
|
|
def get_filename(path):
|
|
filename = os.path.basename(path)
|
|
return filename
|
|
|
|
|
|
def move_files(src_dir, dst_dir):
|
|
for file_name in os.listdir(src_dir):
|
|
src_file = os.path.join(src_dir, file_name)
|
|
dst_file = os.path.join(dst_dir, file_name)
|
|
shutil.move(src_file, dst_file)
|
|
|
|
|
|
def remove_ext(file_name):
|
|
name, ext = os.path.splitext(file_name)
|
|
return name |