import os,re def change_filename(root): ''' 批量修改excel文件名 ''' for root_,_, fs in os.walk(root): for f in fs: tail = f.upper().rspilt('.',1)[-1] if tail in ['XLS','XLSX']: new_name = os.path.join(root_,os.path.basename(root_).replace('-','_') + tail) #文本处理,将—换成_ old_name = os.path.join(root_,f) os.rename(old_name,new_name) def cherk_lost_file(root): ''' 查询缺漏的文件,文件名都遵循某格式:例如 20150211103012_440507_龙湖区_区级_2015年决算.xls 或者 20150211103012_440507_龙湖区_区级_2015_决算.xls 每个地区的文件都要有2015——2017年三份文件才算整齐,需要查漏缺少的文件。 ''' log_file = '缺少的决算报表.log'#注意添加后缀 name_list = os.listdir(root) name_dict = dict() with open(log_file,'a') as f: for file_name in name_list: city_name = re.match(r'\d+_(\d+_\S+.*)_(\d+).*\.xls',file_name).group(1) year = re.match(r'\d+_(\d+_\S+.*)_(\d+).*\.xls',file_name).group(2) if city_name in name_dict.keys(): name_dict[city_name].append(year) else: name_dict[city_name]=[] name_dict[city_name].append(year) for city_name in name_dict.keys(): year_count = name_dict[city_name] if len(year_count) < 3: x = set(year_count) y = set(['2015','2016','2017']) lost = y-x f.write(city_name+'\t'+str(lost)+'\n')