import os.path from log import logger import unzip import file import db root_path = r'F:\Temp\sjry\hj' # 初始化数据库 db_obj = db.DbAction() file_obj = file.Files(root_path) unzip_obj = unzip.UnzipFile() # 开始任务 def start_unzip_task(): result = True for folder in file_obj.get_root_folder_list(): all_file = file_obj.get_all_files(os.path.join(root_path, folder)) # 获取文件夹下的全部文件的原始数据 all_file = file_obj.cate_files(all_file) # 整理文件返回整理后的结果 print(all_file) # 从数据库中获取数据 data = db_obj.get_data_by_id(folder) if data: # 解压 if all_file['handle_zip'] and unzip_obj.unzip(all_file['handle_zip'][0], data['unzip_pwd']): file_obj.del_all_files(all_file['zip']) # 打印未知文件 if all_file['unknown']: logger.info("打印没有处理的文件扩展名:") logger.info(', '.join(all_file['unknown'])) result = False # 检查打印结果 if not (all_file['handle_zip'] and all_file['zip'] and all_file['unknown']): logger.info('全部文件已解压') return result # 整理文件夹 def start_collation_task(): for folder in file_obj.get_root_folder_list(): if db_obj.get_data_by_id(folder): folder_path = os.path.join(root_path, folder) # 清除多余的文件 file_obj.clear_files(folder_path) # 整理无效文件夹 file_obj.move_files(folder_path) # 删除空文件夹 file_obj.remove_empty(folder_path) # 从数据库中获取数据 data = db_obj.get_data_by_id(folder) # 重命名文件夹 name = data['name'] file_obj.rename_root_folder(root_path, name, folder_path, os.path.join(root_path, name)) def main(): n = 5 unzip_result = False while n > 0: unzip_result = start_unzip_task() if unzip_result: break n -= 1 if unzip_result: start_collation_task() if __name__ == '__main__': main()