""" AUTHOR: Khushal P Soonderji DATE: Friday, 26th April, 2024 OBJECTIVE: To provide an easy way to work with files and directories in an synchronous way. REFERENCES: N/A DOWNLOADS: N/A """ # ***************************************************************************************************************** # ***** **** # *** IMPORT *** # ***** **** # ***************************************************************************************************************** # To make sibling directories accessible for imports: import sys sys.path.append(".") sys.path.append("..") # Other system-level dependencies: import os import sys import pathlib import platform # ***************************************************************************************************************** # ***** **** # *** MACROS / ONE-TIME INIT *** # ***** **** # ***************************************************************************************************************** # --- Nothing Yet # ***************************************************************************************************************** # ***** **** # *** VARIABLES *** # ***** **** # ***************************************************************************************************************** # --- Nothing Yet # ***************************************************************************************************************** # ***** **** # *** FUNCTIONS *** # ***** **** # ***************************************************************************************************************** def get_cwd(): if platform.system().lower().find("windows") > -1: return os.getcwd() else: return os.path.split(os.path.realpath(__file__))[0] # --------------------------------------------------------------------------------------------------------------------- def get_parent_directory(path): return pathlib.Path(path).parent.absolute() # --------------------------------------------------------------------------------------------------------------------- def make_directory(path): if not os.path.exists(path): try: os.makedirs(path) except Exception as excp: print("FILE UTILS EXCEPTION (make dir):", excp) return False if os.path.exists(path): return True else: return False # --------------------------------------------------------------------------------------------------------------------- def get_directory_for_file_path(file_path): if os.path.exists(file_path): path_components = os.path.split(file_path) file_name = path_components[-1] if file_name.find(".") > -1: file_path = os.path.join("", *path_components[:-1]) return file_path # --------------------------------------------------------------------------------------------------------------------- def write_file(file_path, file_data, mode = "w"): try: file = open(file_path, mode) file.write(file_data) file.close() return True except Exception as excp: print("FILE UTILS EXCEPTION (write file):", excp) return False # --------------------------------------------------------------------------------------------------------------------- def append_file(file_path, file_data): return write_file(file_path, file_data, mode = "a") # --------------------------------------------------------------------------------------------------------------------- def read_file(file_path, mode = "r", encoding = "utf8"): try: file = open(file_path, mode, encoding = encoding) file_contents = file.read() file.close() return file_contents except Exception as excp: print("FILE UTILS EXCEPTION (read file):", excp) return None # --------------------------------------------------------------------------------------------------------------------- def rename_file(current, new): try: if os.path.isfile(current): os.rename(current, new) return True return False except Exception as excp: print("FILE UTILS EXCEPTION (rename file):", excp) return False # --------------------------------------------------------------------------------------------------------------------- def rename_directory(current, new): try: if os.path.isdir(current): os.rename(current, new) return True return False except Exception as excp: print("FILE UTILS EXCEPTION (rename dir):", excp) return False # --------------------------------------------------------------------------------------------------------------------- def delete_file(file_path): try: if os.path.isfile(file_path): os.remove(file_path) return True return False except Exception as excp: print("FILE UTILS EXCEPTION (delete file):", excp) return False # --------------------------------------------------------------------------------------------------------------------- def delete_directory(directory_path): try: if os.path.exists(directory_path): for root, directories, files in os.walk(directory_path, topdown = False): for file in files: os.remove(os.path.join(root, file)) for directory in directories: os.rmdir(os.path.join(root, directory)) os.rmdir(directory_path) return True return False except Exception as excp: print("FILE UTILS EXCEPTION (delete dir):", excp) return False # --------------------------------------------------------------------------------------------------------------------- def move_file(file_path, new_dir): try: destination = os.path.join(new_dir, os.path.split(file_path)[-1]) os.replace(file_path, destination) return True except Exception as excp: print("FILE UTILS EXCEPTION (move file):", excp) return False # --------------------------------------------------------------------------------------------------------------------- def copy_file(file_path, new_dir): try: destination = os.path.join(new_dir, os.path.split(file_path)[-1]) handle_src = open(file_path, mode = "r") handle_dst = open(destination, mode = "w") stat_src = os.stat(file_path) n_bytes = stat_src.st_size fd_src = handle_src.fileno() fd_dst = handle_dst.fileno() os.sendfile(fd_dst, fd_src, 0, n_bytes) return True except Exception as excp: print("FILE UTILS EXCEPTION (copy file):", excp) return False # --------------------------------------------------------------------------------------------------------------------- def list_files(directory_path, full_path = False): try: path = pathlib.Path(directory_path) files = [entry.name for entry in path.iterdir() if entry.is_file()] if full_path: files = [os.path.join(directory_path, file) for file in files] return files except Exception as excp: print("FILE UTILS EXCEPTION (list files):", excp) return False # --------------------------------------------------------------------------------------------------------------------- def list_dirs( directory_path, full_path = False, raise_exception = False ): try: path = pathlib.Path(directory_path) files = [entry.name for entry in path.iterdir() if entry.is_dir()] if full_path: files = [os.path.join(directory_path, file) for file in files] return files except Exception as excp: if raise_exception: raise print("FILE UTILS EXCEPTION (list dirs):", excp) return [] # ***************************************************************************************************************** # ***** **** # *** MAIN PROGRAM *** # ***** **** # ***************************************************************************************************************** if __name__ == "__main__": pass