前言:在遇见需要给文件改拼音的情况,当数量不多时,手动也行,成千上万时,通过代码修改相对轻松,,比起在网上下载一些不知道带不带病毒的软件,代码版的用起来更放心
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 31 32 33 34 35 36 37 38
| import os from pypinyin import lazy_pinyin
def rename_files(path): files = os.listdir(path)
for file in files: file_path = os.path.join(path, file)
if os.path.isdir(file_path): folder_name = ''.join(lazy_pinyin(file)) new_folder_path = os.path.join(path, folder_name) os.rename(file_path, new_folder_path) print(f"已重命名文件夹:{file_path} -> {new_folder_path}") rename_files(new_folder_path)
else: filename, ext = os.path.splitext(file) new_filename = ''.join(lazy_pinyin(filename)) new_file_path = os.path.join(path, new_filename + ext) os.rename(file_path, new_file_path) print(f"已重命名文件:{file_path} -> {new_file_path}")
def main(): folder_path = r"C:\Users\Lenovo\Desktop\fsdownload\需要处理数据的文件夹" rename_files(folder_path)
if __name__ == "__main__": main()
|
当前文件夹的名字不会被更改,只会改他的子集文件或者子集的子集