参考:https://blog.csdn.net/u012052268/article/details/77823970
繁体转简体.py文件统一目录下放入langconv.py和zh_wiki.py。
https://raw.githubusercontent.com/skydark/nstools/master/zhtools/langconv.py
https://raw.githubusercontent.com/skydark/nstools/master/zhtools/zh_wiki.py
os.walk时候貌似因为文件名已经被修改了,导入递归遍历不完全,多跑几次程序就好了。。。
#繁体转简体.py
from langconv import *
import os,stat
root_path='H:\新建文件夹'
# root_path=r'K:\新建文件夹\test'
def chmod(path):
os.chmod(path, stat.S_IWRITE)
os.chmod(path, stat.S_IRWXO)
os.chmod(path, stat.S_IRWXU)
def convert(root_path):
for root,dirs,files in os.walk(root_path):
for dir in dirs:
chmod(os.path.join(root,dir))
new_dir = Converter('zh-hans').convert(dir)
if new_dir!=dir:
os.rename(os.path.join(root,dir),os.path.join(root,new_dir))
print(os.path.join(root,dir))
for file in files:
chmod(os.path.join(root,file))
new_file = Converter('zh-hans').convert(file)
if new_file!=file:
os.rename(os.path.join(root,file),os.path.join(root,new_file))
print(os.path.join(root,file))
# print(file)
if __name__ == '__main__':
convert(root_path)