import os import copy import shutil from BaseModels.print_funcs import print_ext def del_file(filepath, del_other_ext=None): if os.path.exists(filepath): os.remove(filepath) print_ext(f'- del file {filepath} is done') if del_other_ext: filepath2 = change_file_extension(filepath, del_other_ext) if os.path.exists(filepath2): os.remove(filepath2) print_ext(f'- del file {filepath2} is done') return True print_ext(f'!- file {filepath} not found') return False def del_folder(path): shutil.rmtree(path) return True def check_filepath_exists(path_for_check): if not os.path.exists(path_for_check): return False else: return True def get_file_name_from_filepath(filepath): filepath_list = filepath.split('/') if not filepath_list: filepath_list = filepath.split('\\') if len(filepath_list) > 1: return filepath_list[-1] else: return filepath def get_file_extension(filename, w_point=True): filename_list = filename.split('.') if len(filename_list) > 1: res = filename_list[-1] if w_point: res = '.' + res return res else: return '' def get_filename_wo_ext(filename): filename_list = filename.split('.') if len(filename_list) > 1: return '.'.join(filename_list[:-1]) else: return filename def change_file_extension(filename, new_extension): filename_list = filename.split('.') if len(filename_list) > 1: return '.'.join(filename_list[:-1]) + '.' + new_extension else: return filename + '.' + new_extension def folder_check_and_create(path): path_for_check = copy.copy(path) # if path_for_check[0] != '/': # path_for_check = '/' + path_for_check if not os.path.exists(path_for_check): os.makedirs(path_for_check) # if path[0] != '/': # path_for_check = path_for_check[1:] return path_for_check def get_filename_from_path(filepath, wo_ext=False): f_list = filepath.split('/') if len(f_list) > 1: filename = f_list[-1] else: filename = f_list[0] f_list = filename.split('\\') if len(f_list) > 1: filename = f_list[-1] else: filename = f_list[0] if filename and wo_ext: f_list = filename.split('.') filename = f_list[0] return filename def get_path_wo_filename(filepath): try: f_list = filepath.split('/') if len(f_list) < 2: if filepath[-1] != '/': filepath += '/' return filepath if f_list[-1] == '': if filepath[-1] != '/': filepath += '/' return filepath return '/'.join(f_list[:-1]) + '/' except Exception as e: msg = f'get_path_wo_filename Exception: {e}' print_ext(msg) if filepath[-1] != '/': filepath += '/' return filepath def get_allow_filename(filename, filepath): from os import path, access, R_OK # W_OK for write permission. if '.' in filename: fn_list = filename.split('.') fn_ext = fn_list[-1] fn = '.'.join(fn_list[:-1]) else: fn = filename fn_ext = '' i = 0 full_path = filepath + filename while path.exists(full_path):# and path.isfile(full_path) and access(full_path, R_OK): i += 1 full_path = f'{filepath}{fn}-{str(i)}' if fn_ext: full_path = f'{full_path}.{fn_ext}' if i == 0: filename = f'{fn}' else: filename = f'{fn}-{str(i)}' if fn_ext: filename = f'{filename}.{fn_ext}' return filename def get_filenames_in_path(path): files_list = os.listdir(path) return files_list