def clean_file_path(cls, path: str): """ 清理文件目录 1、批量删除带括号的图片文件(重复下载) 2、删除宣传图片 2、将文件名修改为"产品编号_main/detail_序号"的格式 3、将文件夹按款式进行分类 @param {str} path - 要清理的文件夹 @return {iter_list} - 通过yield返回的处理进度信息清单 [总文件数int, 当前已处理文件数int, 是否成功] """ try: _path = path if not (_path.endswith('/') or _path.endswith('\\')): _path = _path + '/' # 创建分类目录 _class_path = os.path.join(FileTool.get_parent_dir(_path), FileTool.get_dir_name(_path) + '_class') if not os.path.exists(_class_path): FileTool.create_dir(_class_path, exist_ok=True) # 获取目录清单 _dir_list = cls._get_child_dir_list(path, with_root=True) _total = len(_dir_list) _deal_num = 0 # 先返回进度情况 if _total == 0: yield [_deal_num, _total, True] return # 遍历目录执行处理 for _dir in _dir_list: yield [_deal_num, _total, True] cls._clean_file_path(_dir, _class_path) _deal_num += 1 yield [_deal_num, _total, True] except: print('clean_file_path error: %s\r\n%s' % (path, traceback.format_exc())) yield [-1, -1, False]
def _write_product_info_to_xls(cls, path: str, sheet, title: dict, current_row: list): """ 按目录逐个将产品信息写入excel文件> @param {str} path - 要处理的目录 @param {object} sheet - excel的sheet对象 @param {dict} title - 标题清单 @param {list} current_row - 当前行 """ # 先处理自己 _info_file = os.path.join(path, 'info.json') if os.path.exists(_info_file): # 有信息文件才处理 _info = dict() with open(_info_file, 'rb') as f: _eval = str(f.read(), encoding='utf-8') _info = eval(_eval) # 产品编号和产品目录 _product_num = FileTool.get_dir_name(path) sheet.write(current_row[0], 0, _product_num) sheet.write(current_row[0], 1, path) # 逐个信息项写入 for _key in _info.keys(): if _key in title.keys(): sheet.write(current_row[0], title[_key], _info[_key]) else: # 要新增列标题 _col = len(title) + 2 title[_key] = _col sheet.write(0, _col, _key) # 写入信息值 sheet.write(current_row[0], _col, _info[_key]) # 换到下一行 current_row[0] += 1 # 处理子目录 _dirs = FileTool.get_dirlist(path) for _dir in _dirs: cls._write_product_info_to_xls(_dir, sheet, title, current_row)
def _clean_file_path(cls, path: str, class_path: str): """ 清理当前目录文件 @param {str} path - 要处理的目录地址 @param {str} class_path - 类目录 """ # 处理自身目录,先获取商品信息 _info = dict() _info_file = os.path.join(path, 'info.json') if os.path.exists(_info_file): with open(_info_file, 'rb') as f: _eval = str(f.read(), encoding='utf-8') _info = eval(_eval) # 判断是否不处理 _shop_name = _info['店名'] # if _info['款式'] == '挂件' and _info['挂件类型'] == '': # return # 遍历文件进行处理 _product_num = FileTool.get_dir_name(path) _files = FileTool.get_filelist(path) _order = 1 for _file in _files: _file_ext = FileTool.get_file_ext(_file).lower() if _file_ext not in ['jpg', 'jpeg', 'png', 'bmp']: # 不是合适的文件类型 continue # 判断是否有括号 if _file.find('(') >= 0: FileTool.remove_file(_file) continue # 判断是否匹配上要删除的图片大小 if _shop_name in DEL_SHOP_PIC_SIZE.keys() and os.path.getsize( _file) in DEL_SHOP_PIC_SIZE[_shop_name]: FileTool.remove_file(_file) continue # 修改文件名 if not FileTool.get_file_name(_file).startswith(_product_num): os.rename( _file, os.path.join( path, '%s_%s_%d.%s' % (_product_num, 'main' if _file.find('主图') >= 0 or _file.find('main') >= 0 else 'detail', _order, _file_ext))) # 下一个文件 _order += 1 # 移动文件夹到指定的分类目录 _class_path = _info['款式'] if _class_path in PROP_TYPE_TRAN_DICT.keys(): _class_path = PROP_TYPE_TRAN_DICT[_info['款式']] shutil.move(path, os.path.join(class_path, _class_path, _product_num)) # 处理完成,返回 return