import os def rename_files(directory, prefix): for count, filename in enumerate(os.listdir(directory)): new_name = f"{prefix}_{str(count)}{os.path.splitext(filename)[1]}" os.rename(os.path.join(directory, filename), os.path.join(directory, new_name)) print(f"已重命名: {filename} -> {new_name}") # 堆代码 duidaima.com # 示例用法 directory_path = "files_folder" rename_files(directory_path, "document")该脚本会将指定目录下的所有文件重命名为指定前缀加上编号的格式,适用于批量处理文档或图片。