import os
# 递归获取文件
def rename_file(path,new_file):
for oldname in new_file:
oldname = str(oldname)
if '【程序员教程吧 论 坛 www.cxyjc8.com】' in oldname:
newname = oldname.replace('【程序员教程吧 论 坛 www.cxyjc8.com】', '')
oldname = os.path.join(path, oldname)
newname = os.path.join(path, newname)
try:
os.rename(oldname, newname)
except Exception as e:
print(e)
print('rename file fail\r\n')
def recursion_get_file(paths):
path = paths + "/"
first_dir_list = os.listdir(path)
print(first_dir_list)
new_file = [file for file in first_dir_list if os.path.isfile(path + file)]
if new_file:
# 重命名
rename_file(path, new_file)
dirs = [str(path + file) for file in first_dir_list if os.path.isdir(path + file)]
if dirs:
for dir in dirs:
recursion_get_file(dir)
if __name__=="__main__":
# 需要重命名文件根目录
root_paths=r'E:\07_2020黑马java会员版2.1完整版\12-就业课(2.1)-消息中间件-RabbitMQ - 副本'
recursion_get_file(paths=root_paths)
12-02
1027
07-07
2万+