def rename_file(file_dir, file_name, new_file_name): old_path = join(file_dir, file_name) # 断言保证 assertor.file_exist_assert(old_path) new_path = join(file_dir, new_file_name) os.rename(old_path, new_path)
def un_compression(filename): assertor.file_exist_assert(filename) file_root = os.path.splitext(filename)[0] judge_mkdir(file_root) un_comp_file = Archive(filename) un_comp_file.extractall(file_root)
def get_list_from_list_file(file_path: str): assertor.file_exist_assert(file_path) with open(file_path, 'r') as f: data = f.read() rst_list = eval(data) return rst_list
def save_list_to_list_file(input_list: list, save_path: str): assertor.type_assert(input_list, list) assertor.file_exist_assert(file_path) with open(file_path, 'w') as f: f.write(str(input_list)) return rst_list
def copy_file(old_dir, old_name, new_dir, new_name): old_path = join(old_dir, old_name) # 断言保证 assertor.file_exist_assert(old_path) assertor.path_exist_assert(new_dir) new_path = join(new_dir, new_name) shutil.copyfile(old_path, new_path)
def zip_file(file_dir: str, file_name: str, target_dir: str, target_name: str): file_path = join(file_dir, file_name) assertor.file_exist_assert(file_path) judge_mkdir(target_dir) with zipfile.ZipFile(join(target_dir, target_name), 'w', zipfile.ZIP_DEFLATED) as f: print('begin to zip file') f.write(file_path) print('done')
def copy_dir(old_dir, old_dir_name, new_dir, new_dir_name): old_path = join(old_dir, old_dir_name) # 断言保证 assertor.file_exist_assert(old_path) if not os.path.exists(new_dir): new_path = join(new_dir, new_dir_name) shutil.copytree(old_path, new_path) else: print("================================================") print("WARNING the new dir exist, not running copy dir") print("THIS WARNING FROM FUNCTION -> copy_dir") print("================================================")
def _get_config_parser(self, *config_path_tuple: tuple): """ 得到config parser :param config_path_tuple: tuple :return: config """ file_path = yy.io.join(*config_path_tuple) # 断言保证 assertor.file_exist_assert(file_path) config = configparser.ConfigParser() config.read(file_path) return config
def get_json_as_dict(file_path: str): """ 通过json文件读取,返回dict :param file_path: str :return: data_dict: dict """ # 断言保证 assertor.file_exist_assert(file_path) with open(file_path, 'r') as f: json_data = f.read() json_data = json.loads(json_data) data_dict = dict(json_data) return data_dict
def del_file(file_dir: str, file_name: str) -> None: """ 删除目标文件 :param file_dir: str :param file_name: str :return: None """ # 断言保证 assertor.type_assert(file_dir, str) assertor.type_assert(file_name, str) file_path = join(file_dir, file_name) # 断言保证 assertor.file_exist_assert(file_path) os.remove(file_path)
def get_hdf5_as_dict(*paths: tuple) -> dict: """ 通过hdf5文件读取,返回dict :param paths: tuple :return: data_dict: dict """ # 断言保证 assertor.tuple_type_assert(paths, str) file_path = yy.io.join(*paths) # 断言保证 assertor.file_exist_assert(file_path) data_dict = {} with h5py.File(file_path, 'r') as f: for key, value in f.items(): data_dict[key] = value.value return data_dict
def transfer_npy_to_hdf5(npy_file_path: str, save_file_dir: str, save_file_name: str, key_name: str) -> None: """ 读取npy文件, 转换为hdf5文件保存 :param npy_file_path: str :param save_file_dir: str :param save_file_name: str :param key_name: str :return: None """ # 断言保证 assertor.file_exist_assert(npy_file_path) assertor.path_exist_assert(save_file_dir) # 读取npy数据 npy_data = np.load(npy_file_path) # 变为dict rst_dict = {key_name: npy_data} # 存储为hdf5 save_dict_as_hdf5(save_file_dir, save_file_name, rst_dict)
def judge_del_copy_dir(old_dir, new_dir): # 断言保证 assertor.file_exist_assert(old_dir) judge_del_dir(new_dir) shutil.copytree(old_dir, new_dir)