# --coding:utf-8-- ''' 文件操作 ''' import os import shutil from loguru import logger from shutil import copyfile def save_file(file: bytes, filename): ''' bytes保存文件 :param file: :param filename: :return: ''' try: with open(filename, "wb") as fw: fw.write(file) fw.close() return True except Exception as e: logger.warning("保存pdf失败-->%s" % e) return False def generate_directory(dir: str) -> bool: ''' :param dir: :return: ''' try: if not os.path.exists(dir): os.makedirs(dir) os.chmod(dir, 0o777) except Exception: return False return True def del_directory(dir: str): if os.path.exists(dir): shutil.rmtree(dir) def file_copy(source_path: str, target_path: str): """ 文件copy到目标文件夹 :param source_path: 文件原路径 :param target_path: 目标文件夹 :return: """ try: copyfile(source_path, target_path) except IOError as e: logger.warning("Unable to copy file. %s" % e) return False return True