批量修改文件名的小脚本
前几天下了一个:程序猿必知必会-MySQL 8.0详解与实战,里面的文件只有编号没有内容,不方便查看,用python搞了个小脚本批量重命名。import os
path = "D:\BaiduYunDownload\【瑞客论坛 www.ruike1.com】程序猿必知必会-MySQL 8.0详解与实战"
filenames = os.listdir(path)
# for filename in filenames:
# filename = path + '\\' + filename
# print(os.path.isfile(filename))
# print(filenames)
with open('listname.txt', encoding='utf-8', mode='r') as f:
listnames = f.readlines()
for filename in filenames:
filetype = '.mp4'
splitedfilename = filename.split('【')
sfn0 = splitedfilename
for listname in listnames:
# splitedliciwstname = listname.split(' ')
sln0, *sln1 = listname.split(' ')
ssln1 = ''.join(sln1)
# sln0 = splitedlistname
# *sln1 = splitedlistname
if sfn0 == sln0:
newfilename = sfn0 + ssln1 + filetype
newfilename = newfilename.replace('\n', '').strip()
newfilename = path + '\\' + newfilename
filename = path + '\\' + filename
print(os.path.isfile(filename))
print(newfilename)
os.rename(filename, newfilename)主要思路是找出原始文件名和官网上的文件名重复的部分,匹配后用官网正确的文件名替换。
强烈支持楼主ing……
页:
[1]