(承接"公共标,特殊标"的结构)1.对于指定父目录结构,每个文件夹抽指定数量的随机图片。
import os import shutil import random def find_image_folders(parent_directory): # 查找包含图片的文件夹 image_folders = [] for root, dirs, files in os.walk(parent_directory): if any(file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')) for file in files): image_folders.append(root) return image_folders def copy_random_images(source_folder, target_folder, number_of_images): # 复制随机图片到目标文件夹 images = [file for file in os.listdir(source_folder) if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))] if not images or number_of_images <= 0: return selected_images = random.sample(images, min(number_of_images, len(images))) for image in selected_images: shutil.copy(os.path.join(source_folder, image), os.path.join(target_folder, image)) def main(): parent_directory = input("请输入源父目录地址: ") number_of_images = int(input("请输入每个文件夹抽取图片的数量: ")) target_folder = input("请输入目标文件夹的地址: ") if not os.path.exists(target_folder): os.makedirs(target_folder) image_folders = find_image_folders(parent_directory) for folder in image_folders: copy_random_images(folder, target_folder, number_of_images) print("图片复制完成。") if __name__ == "__main__": main()
2.针对文件夹,去除图片第一个逗号及第一个逗号前的内容
import os def clean_file_names(parent_directory): if not os.path.isdir(parent_directory): print("指定的目录不存在,请检查路径。") return try: os.chdir(parent_directory) for file_name in os.listdir(): if ',' in file_name: new_name = file_name.split(',', 1)[1].strip() original_new_name = new_name # 检查是否存在同名文件,并调整新文件名直到它是唯一的 counter = 1 while os.path.exists(new_name): # 添加计数器到文件名以避免重复 name, ext = os.path.splitext(original_new_name) new_name = f"{name} ({counter}){ext}" counter += 1 os.rename(file_name, new_name) print(f"Renamed '{file_name}' to '{new_name}'") else: print(f"No change for '{file_name}'") except Exception as e: print(f"发生错误: {e}") if __name__ == "__main__": while True: parent_directory = input("请输入父目录名称(或输入'exit'退出):") if parent_directory.lower() == 'exit': break clean_file_names(parent_directory) print("图片文件名清理完成。\n")
标签:网页,name,parent,os,文件夹,file,directory,new,伪造 From: https://www.cnblogs.com/zly324/p/17920058.html