在文员道路上想办法偷懒的我,文件名前后缀批量增删,文件名空格处理,删除特殊文件,文件名特殊字符批量删除。直接上代码,大佬勿喷,给点建议,帮助小弟提神自我。
import os import string import sys import re def rename_cut_front(dir_path, cut_index): file_num = 0 file_rename_fail = 0 file_names = os.listdir(dir_path) for name in file_names: sub_path = os.path.join(path, name) if os.path.isfile(sub_path): file_num = file_num + 1 # 统计文件数量 # print(file_num) if cut_index in name : for s in name: if s == cut_index: # 索引字符 index_num = name.index(s) # index_num为要删除的位置索引 # 采用字符串的切片方式删除编号 rename_enable = 1 new_name = name[index_num + 1:] for name_check in file_names: if new_name == name_check : print(os.path.join(path, name) + " the file is repeated after modification!") file_rename_fail = file_rename_fail +1 rename_enable =0 break if rename_enable == 1 : os.rename(os.path.join(path, name), os.path.join(path, new_name)) break # 重命名成功,跳出内循环 else : file_rename_fail = file_rename_fail + 1 continue print(" Combined files : %d PS File Renamer: %d Fail: %d " %(file_num,file_num-file_rename_fail,file_rename_fail) ) def clean_space(text): ######转自六神就是我 """" 处理多余的空格 """ match_regex = re.compile(u'[\u4e00-\u9fa5。\.,,::《》、\(\)()]{1} +(?<![a-zA-Z])|\d+ +| +\d+|[a-z A-Z]+') should_replace_list = match_regex.findall(text) order_replace_list = sorted(should_replace_list,key=lambda i:len(i),reverse=True) for i in order_replace_list: if i == u' ': continue new_i = i.strip() text = text.replace(i,new_i) return text def rename_add_front(dir_path, before_content): file_names = os.listdir(dir_path) for name in file_names: # 如果某个文件名在file_names内 old_name = path + '\\' + name # 获取旧文件的名字,注意名字要带路径名 new_name = path + '\\' + before_content + name # 定义新文件的名字,这里给每个文件名前加了前缀 a_ os.rename(old_name, new_name) # 用rename()函数重命名 print(new_name) # 打印新的文件名字 def rename_add_behind(dir_path, follows_content): file_names = os.listdir(dir_path) for name in file_names: # 如果某个文件名在file_names内 old_name = dir_path + '\\' + name # 获取旧文件的名字,注意名字要带路径名 portion = os.path.splitext(name) # 将文件名拆成名字和后缀 new_name = dir_path + '\\' + portion[0] + follows_content + portion[1] os.rename(old_name, new_name) # 用rename()函数重命名 print(new_name) # 打印新的文件名字 def file_del(dir_path, postfix): file_nums = 0 remove_num = 0 file_names = os.listdir(dir_path) for name in file_names: # 如果某个文件名在file_names内 sub_path = os.path.join(path, name) if os.path.isfile(sub_path): file_nums = file_nums + 1 # 统计文件数量 old_name = dir_path + '\\' + name # 获取旧文件的名字,注意名字要带路径名 portion = os.path.splitext(name) # 将文件名拆成名字和后缀 if portion[1] == postfix: remove_num = remove_num + 1 os.remove(old_name) print(old_name + " has been deleted!") # 打印新的文件名字 print(" Combined files : %d Remove files: %d " % (file_nums, remove_num)) def delete_space(dir_path): file_num = 0 file_rename_fail = 0 file_names = os.listdir(dir_path) for name in file_names: sub_path = os.path.join(path, name) if os.path.isfile(sub_path): file_num = file_num + 1 # 统计文件数量 # print(file_num) portion = os.path.splitext(name) # 将文件名拆成名字和后缀 rename_enable = 0 Original_name = portion[0] if ' ' in Original_name : rename_enable = 1 new_name = Original_name.strip(' ') + portion[1] #去除文件首尾空格 new_name = clean_space(new_name) #去除文件空格 else: continue if rename_enable == 1 : for name_check in file_names: if new_name == name_check: print(os.path.join(path, name) + " the file is repeated after modification!") file_rename_fail = file_rename_fail + 1 rename_enable = 0 break if rename_enable == 1: os.rename(os.path.join(path, name), os.path.join(path, new_name)) print(" Combined files : %d PS File Renamer: %d Fail: %d " % (file_num, file_num - file_rename_fail, file_rename_fail)) def delete_special_characters(dir_path,special_characters): file_num = 0 file_rename_fail = 0 file_names = os.listdir(dir_path) for name in file_names: sub_path = os.path.join(path, name) if os.path.isfile(sub_path): file_num = file_num + 1 # 统计文件数量 # print(file_num) portion = os.path.splitext(name) # 将文件名拆成名字和后缀 rename_enable = 0 Original_name = portion[0] if special_characters in Original_name: rename_enable = 1 new_name = Original_name.replace(special_characters, "") + portion[1] else: continue # print(new_name) # 打印新的文件名字 if rename_enable == 1 : for name_check in file_names: if new_name == name_check: print(os.path.join(path, name) + " the file is repeated after modification!") file_rename_fail = file_rename_fail + 1 rename_enable = 0 break if rename_enable == 1: os.rename(os.path.join(path, name), os.path.join(path, new_name)) print(" Combined files : %d PS File Renamer: %d Fail: %d " % (file_num, file_num - file_rename_fail, file_rename_fail)) if __name__ == "__main__": # path为批量文件的文件夹的路径 path = 'C:\\Users\ZKK\Desktop\file_test' # rename_add_front(path, "附件-X ") #文件名前缀批量增加 # rename_cut_front(path, 'X') #文件名前缀批量删除 # rename_add_behind(path, "-截图") #文件名后缀批量增加 # file_del(path,'.pdf') #批量删除特殊文件 # delete_space(path) #文件名空格批量处理 # delete_special_characters(path, "-截图") # 文件名内特殊字符批量删除