将不想要的部分去除
import os
def rename_file(file_dir):
for root, dirs, files in os.walk(file_dir):
print(1,root) #当前目录路径
print(2,dirs) #当前路径下所有子目录list
print(3,files) #当前路径下所有非目录子文件 list
print("")
# 1、先处理当前目录下的文件
for file_name in files:
if "[www.17zixueba.com]" in file_name:
old_file_path = root + "\\" + file_name
new_file_name = file_name.replace('[www.17zixueba.com]', '')
new_file_path = root + "\\" + new_file_name
print('old_file_path:',old_file_path)
print('new_file_path:',new_file_path)
os.rename(old_file_path, new_file_path)
# 2、递归处理目录下的子目录及子目录中的文件
for dir_name in dirs:
old_dir_path = root + "\\" + dir_name
if "[www.17zixueba.com]" in dir_name:
new_dir_name = dir_name.replace('[www.17zixueba.com]', '')
new_dir_path = root + "\\" + new_dir_name
print("old_dir_path:",old_dir_path)
print("new_dir_path:",new_dir_path)
os.rename(old_dir_path, new_dir_path)
dir_path = new_dir_path
else:
dir_path = old_dir_path
rename_file(dir_path)
path = "I:\技术\教程\Python\数据分析与挖掘\python数据分析全套"
# path = "E:\python数据分析全套"
rename_file(path)