import os import re import shutil src_paths = [ "../ccbi", "../关卡", "../关卡/关卡模板", "../ccb/钉子", "../ccb/盘子", "../ccb/目标", "../config", "../plist", "../png_plist", "../png_plist/sg_gameui", "../png_plist/sg_items", "../img_single", "../轮廓/sg_宝石轮廓", "../轮廓/sg_盘子轮廓" ] dst_dir = "./ScrewGame1-desktop.app/Contents/Resources" #tools_dir = "/Users/red/ScrewGame/Resources/res_ScrewGame/tools" # 需要拷贝的文件的后缀 required_suffixes = { ".redream", ".red", ".json", ".png", ".webp", ".tps" } def get_suffix(path: str) -> str: suffixes = re.findall( r"(\.[a-zA-Z^/]+)$", path ) if not len(suffixes): # print(f"{path}中没有找到suffix") return "" else: return suffixes[0] # 对比看看哪些文件需要删除 def compare_and_delete_files(file_names: list) -> None: dst_dir_full_path = os.path.join(os.getcwd(), dst_dir) dst_file_names = os.listdir(dst_dir_full_path) file_deleted = [] for file in dst_file_names: if get_suffix(file) in required_suffixes: if not file in file_names: file_deleted.append(file) # print(file_deleted) cnt = 0 for file in file_deleted: full_path = os.path.join(dst_dir_full_path, file) os.remove(full_path) cnt += 1 print(f"{cnt} file(s) removed") def find_files(path: str) -> list: files = [] if os.path.exists(path) and os.path.isdir(path): for file in os.listdir(path): if file != ".DS_Store": if not os.path.isdir(file): files.append(os.path.join(path, file)) else: print(f"{path} not exits or is not a directory.") return files def get_file_name(path: str): pattern = r"([^/]+\.[^/]+)$" matches = re.finditer(pattern, path) ret = [] for match in matches: ret.append(match.group()) return ret def copy_files(src: str, dst: str) -> None: shutil.copy2(src, dst) if __name__ == "__main__": this_file_path = os.path.dirname(os.path.abspath(__file__)) os.chdir(this_file_path) # os.chdir(tools_dir) # print(get_test_path()) files = [] cwd_full_path = os.getcwd() for dir in src_paths: # print(dir) files_in_dir = find_files(dir) # print(files_in_dir) files.append(files_in_dir) files = [ file for file_list in files for file in file_list ] # print(files) file_names = [] file_name_to_remove_from_files = [] for file in files: if not os.path.isdir(file): file_name_list = get_file_name(file) if len(file_name_list) == 1: file_names.append(file_name_list[0]) else: print(f"err: {file}匹配的文件名结果不止一个") else: file_name_to_remove_from_files.append(file) # print(file_names) for file in file_name_to_remove_from_files: files.remove(file) compare_and_delete_files(file_names) if len(files) != len(file_names): print(f"{len(files)}个file: {files}") print(f"{len(file_names)}个file_names: {file_names}") print("长度不统一, 检查一下") else: dst_dir_full_path = os.path.join(os.getcwd(), dst_dir) cnt = 0 for i in range(len(files)): src_full_path = os.path.join(os.getcwd(), files[i]) dst_full_path = os.path.join(dst_dir_full_path, file_names[i]) shutil.copy2(src_full_path, dst_full_path) cnt += 1 print(f"{cnt} file(s) copied")