import os def process_images_interactively(): # Ask the user for the parent directory address parent_dir = input("请输入父目录地址: ") print(f"正在处理的目录: {parent_dir}") # Walk through the directory structure for subdir, dirs, files in os.walk(parent_dir): for file in files: # Check if the file is an image and contains a comma if ',' in file: # Split the filename from its extension name, ext = os.path.splitext(file) # Find the first comma and get the content before it content_a = name.split(',')[0] # Split the content A into words and remove the first word words = content_a.split() if len(words) > 1: # Construct the new name avoiding double commas modified_name_part = ' '.join(words[1:]) + ', ' + content_a rest_of_name = name[len(content_a):].lstrip(', ') # Remove leading commas and spaces modified_name = modified_name_part + ', ' + rest_of_name + ext # Construct the old and new file paths old_file_path = os.path.join(subdir, file) new_file_path = os.path.join(subdir, modified_name) # Rename the file os.rename(old_file_path, new_file_path) print(f"已将 '{file}' 重命名为 '{modified_name}'") print("处理完成。") # 在本地环境运行此函数以开始处理。 process_images_interactively()
标签:name,modified,content,修改,名称,file,path,os,地名 From: https://www.cnblogs.com/zly324/p/17920813.html