网上有些资源文件名上一般带有广告,导致文件名很长且影响阅读, 可以类似这样
import os
pth = r'D:\BaiduYunDownload\萧井陌WEB前端课程'
def rename(pth):
for file in os.listdir(pth):
xfile = os.path.join(pth,file)
if os.path.isdir(xfile):
rename(xfile)
else:
i = None; j = None
if ('[' in file and ']' in file):
i = file.index('[')
j = file.index(']')
if ('【' in file and '】' in file):
i = file.index('【')
j = file.index('】')
file = file[:i] + file[j+1:]
nfile = os.path.join(pth,file)
os.rename(xfile,nfile)
rename(pth)