1 安装
不需要什么安装方法,只需要把这两个文件下载下来,保存到与代码同一目录下即可
https://raw.githubusercontent.com/skydark/nstools/master/zhtools/langconv.py
https://raw.githubusercontent.com/skydark/nstools/master/zhtools/zh_wiki.py
2.使用
将一个目录下的所有文件夹及文件的名字都转换成中文简体并且去除中间的空格
import os
from langconv import *
def simple2tradition(line):
# 将简体转换成繁体
line = Converter('zh-hant').convert(line)
return line
def tradition2simple(line):
# 将繁体转换成简体
line = Converter('zh-hans').convert(line)
return line
def renameDirTradition2simple(rootFolder):
for rt, dirs, files in os.walk(rootFolder):
toString = rt + '\n'
toString = toString + " ==============dirs===============" + '\n'
for folder in dirs:
dirPath = os.path.join(rt, folder)
newDirPath = os.path.join(rt, tradition2simple(folder.replace(" ", "")))
os.rename(dirPath, newDirPath)
toString = toString + " rename " + dirPath + " [to] " + newDirPath + '\n'
renameDirTradition2simple(newDirPath)
toString = toString + " ==============files===============" + '\n'
for f in files:
filePath = os.path.join(rt, f)
newFilePath = os.path.join(rt, tradition2simple(f.replace(" ", "")))
os.rename(filePath, newFilePath)
toString = toString + " rename " + filePath + " [to] " + newFilePath + '\n'
print(toString)
break
if __name__ == '__main__':
root = r"E:\中文简体繁体" #请事先把含有中文繁体名字的文件夹及文件放到root目录下
renameDirTradition2simple(root)