1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import os
def batch_rename(folder_path, file_extension, prefix): if not os.path.exists(folder_path): print(f"文件夹 {folder_path} 不存在") return
files = os.listdir(folder_path)
for filename in files: if filename.endswith(file_extension): new_name = prefix + filename old_file = os.path.join(folder_path, filename) new_file = os.path.join(folder_path, new_name) os.rename(old_file, new_file) print(f"已将 {old_file} 重命名为 {new_file}")
folder_path = '/path/to/your/folder' file_extension = '.txt' prefix = 'prefix_' batch_rename(folder_path, file_extension, prefix)
|