{"id":21997675,"url":"https://github.com/osfunapps/os-file-handler-py","last_synced_at":"2025-03-23T04:45:09.498Z","repository":{"id":57449720,"uuid":"307069118","full_name":"osfunapps/os-file-handler-py","owner":"osfunapps","description":"this module contains file handling functions to implement in a Python project.","archived":false,"fork":false,"pushed_at":"2021-02-07T13:59:34.000Z","size":9,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-28T11:32:14.472Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/osfunapps.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-10-25T09:58:25.000Z","updated_at":"2021-02-07T13:59:36.000Z","dependencies_parsed_at":"2022-09-26T17:31:09.519Z","dependency_job_id":null,"html_url":"https://github.com/osfunapps/os-file-handler-py","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osfunapps%2Fos-file-handler-py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osfunapps%2Fos-file-handler-py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osfunapps%2Fos-file-handler-py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osfunapps%2Fos-file-handler-py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/osfunapps","download_url":"https://codeload.github.com/osfunapps/os-file-handler-py/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245056902,"owners_count":20553854,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-29T22:17:39.014Z","updated_at":"2025-03-23T04:45:09.471Z","avatar_url":"https://github.com/osfunapps.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"Introduction\n------------\n\nthis module contains file handling functions to implement in a Python project.\n\n## Installation\nInstall via pip:\n\n    pip install os-file-handler\n\n\n## Usage       \nRequire FileHandler:\n```python        \nimport ostools.file_handler as fh\n```\n\n# FileHandler\n```python\n# will return the content of a directory (full paths)\ndef get_dir_content(dir_path, recursive=False, collect_dirs=True, collect_files=True, ignored_files_arr=['.DS_Store'], sort_by_name=True):\n    f_list = []\n    d_list = []\n    for path, dirs, files in os.walk(dir_path):\n        if collect_dirs:\n            if not recursive:\n                d_list = append_path_to_list(path, dirs)\n                if collect_files:\n                    f_list = append_path_to_list(path, files)\n                break\n            d_list += append_path_to_list(path, dirs)\n        if collect_files:\n            if not recursive:\n                f_list = append_path_to_list(path, files)\n                break\n\n            f_list += append_path_to_list(path, files)\n\n    f_list = list(filter(lambda x: get_file_name_from_path(x) not in ignored_files_arr, f_list))\n    if f_list:\n        if sort_by_name:\n            f_list.sort()\n        if d_list:\n            if sort_by_name:\n                d_list.sort()\n            return [d_list, f_list]\n        return f_list\n    return d_list\n\n\n# will append a base path to every file in a file list\ndef append_path_to_list(base_path, file_list):\n    full_path_file_list = []\n    for file in file_list:\n        full_path_file_list.append(os.path.join(base_path, file))\n    return full_path_file_list\n\n\n# will return the name of the last dir name from a path\ndef get_dir_name(dir_path):\n    return os.path.basename(os.path.normpath(dir_path))\n\n\n# will split path to arr of dirs\ndef split_path(path):\n    path = os.path.normpath(path)\n    return path.split(os.sep)\n\n\n# will create a file\ndef create_file(path, content: list = None):\n    with open(path, 'w') as f:\n        if content:\n            f.writelines(content)\n\n\n# will create a file from bytes\ndef bytes_to_file(output_path, data: bytes):\n    out_file = open(output_path, \"wb\")\n    out_file.write(data)\n    out_file.close()\n\n\n# will return the extension of a file from a file path\ndef get_extension_from_file(file):\n    _, file_extension = os.path.splitext(file)\n    return file_extension\n\n\n# will return the file name from a file path\ndef get_file_name_from_path(file, with_extension=True):\n    import ntpath\n    filename = ntpath.basename(file)\n    if not with_extension:\n        last_dot_idx = filename.rfind('.')\n        filename = filename[:last_dot_idx]\n    return filename\n\n\n# will remove a directory\ndef remove_dir(path):\n    if os.path.isdir(path):\n        shutil.rmtree(path)\n\n\n# will remove a bunch of files in a list\ndef remove_files(file_list):\n    for file in file_list:\n        remove_file(file)\n\n\ndef copy_file(src, dst, create_path_if_needed=True):\n    \"\"\"\n    Will copy a file to a dest.\n\n    param create_path_if_needed: set to true if the parent directory could not exists and you want to create it\n    \"\"\"\n    if not is_dir_exists(get_parent_path(dst)) and create_path_if_needed:\n        create_dir(get_parent_path(dst))\n\n    shutil.copy(src, dst)\n\n\n# will duplicate a file to the same dir with _temp at the end of it's name\ndef copy_to_temp_file(src):\n    file_full_name = get_file_name_from_path(src)\n    temp_name = file_full_name[0: file_full_name.rfind('.')] + '_temp'\n    extension = get_extension_from_file(file_full_name)\n    temp_dest = get_parent_path(src) + '/' + temp_name + extension\n    copy_file(src, temp_dest)\n    return temp_dest\n\n\n# will return the parent path of a file\ndef get_parent_path(file):\n    from pathlib import Path\n    return str(Path(file).parent)\n\n\n# will copy a list of files to a dir\ndef copy_list_of_files(files_list, dst):\n    for file in files_list:\n        copy_file(file, dst, create_path_if_needed=True)\n\n\n# will rename a file or a directory\ndef rename(src, dst):\n    os.rename(src, dst)\n\n\n# will search for a file in a path by prefix, suffix, full name with/without an extension\ndef search_file(path_to_search, full_name=None, prefix=None, suffix=None, by_extension=None, recursive=True):\n    from pathlib import Path\n    files = []\n    search_queue = ''\n    if recursive:\n        search_queue = '**/'\n    if full_name:\n        search_queue += full_name\n        if by_extension:\n            search_queue += by_extension\n    else:\n        if prefix:\n            search_queue += f'{prefix}'\n            if suffix:\n                search_queue += f'*{suffix}'\n            else:\n                if not by_extension:\n                    search_queue += '*'\n        else:\n            if suffix:\n                search_queue += f'*{suffix}'\n        if by_extension:\n            search_queue += f'*{by_extension}'\n        else:\n            search_queue += f'.*'\n\n    for filename in Path(path_to_search).glob(search_queue):\n        files.append(str(filename))\n    return files\n\n\n# will search for a directory in a path by full name or prefix or/and suffix\ndef search_dir(path_to_search, full_name=None, prefix=None, suffix=None, recursive=True):\n    from pathlib import Path\n    files = []\n    search_queue = ''\n    if recursive:\n        search_queue = '**/'\n\n    if full_name:\n        search_queue += full_name\n    elif prefix:\n        search_queue += f'{prefix}'\n        if suffix:\n            search_queue += f'*{suffix}'\n        else:\n            search_queue += '*'\n    elif suffix:\n        search_queue += f'*{suffix}'\n\n    for filename in Path(path_to_search).glob(search_queue):\n        if is_dir_exists(filename):\n            files.append(str(filename))\n    return files\n\n\ndef replace_line_for_line(file, line_for_line_dict):\n    \"\"\"\n    Will replace a line for a line in a file.\n    Notice: this function use contains() and not ==.\n\n    Args:\n        param file:\n        param line_for_line_dict:\n    \"\"\"\n    lines = []\n    with open(file, \"r\") as f:\n        for line in f:\n            appended = False\n            for key, val in line_for_line_dict.items():\n                if key in line:\n                    lines.append(val + '\\n')\n                    appended = True\n            if not appended:\n                lines.append(line)\n\n    with open(file, \"w\") as f:\n        f.writelines(lines)\n\n\n# is write permission granted\ndef is_file_write_permission_granted(file_path):\n    return os.access(file_path, os.W_OK)\n\n\n# is read permission granted\ndef is_file_read_permission_granted(file_path):\n    return os.access(file_path, os.R_OK)\n\n\n# is file exists\ndef is_file_exists(file_path):\n    return os.path.exists(file_path)\n\n\n# is directory exists\ndef is_dir_exists(dir_path):\n    return os.path.isdir(dir_path)\n\n\ndef is_dir_empty(dir_path):\n    return len(os.listdir(dir_path)) == 0\n\n\n# will copy a directory\ndef copy_dir(src, dst, ignore_patterns_str='.DS_Store', overwrite_content_if_exists=False):\n    from shutil import copytree, ignore_patterns\n    if is_dir_exists(dst):\n        for idx_dir in get_dir_content(src, recursive=True, collect_dirs=True, collect_files=False):\n            dst_dir_paths = idx_dir.replace(f'{src}/', '')\n            dir_dst = os.path.join(dst, dst_dir_paths)\n            copy_dir(idx_dir, dir_dst, overwrite_content_if_exists=overwrite_content_if_exists)\n\n        for idx_file in get_dir_content(src, recursive=True, collect_dirs=False, collect_files=True):\n            f_path = dst_dir_paths = idx_file.replace(f'{src}/', '')\n            file_dst = os.path.join(dst, f_path)\n            copy_file(idx_file, file_dst, create_path_if_needed=True)\n    else:\n        copytree(src, dst, ignore=ignore_patterns(ignore_patterns_str))\n\n\n# will clear the content of a directory\ndef clear_dir_content(dir_path):\n    shutil.rmtree(dir_path)\n    create_dir(dir_path)\n\n\n# will create a directory. If dir exists, do nothing\ndef create_dir(dir_path):\n    if not is_dir_exists(dir_path):\n        os.makedirs(dir_path)\n\n\n# will remove all of the files with a given extension\ndef remove_all_files_with_extension(path, ext):\n    import glob\n    if ext[0] == '.':\n        ext = ext[1:]\n    for f in glob.glob(path + \"/*.\" + ext):\n        remove_file(f)\n\n\n# will remove a single file\ndef remove_file(file):\n    if is_file_exists(file):\n        os.remove(file)\n\n\n# will check if line exists in a file\ndef is_line_exists_in_file(file, line_to_find):\n    with open(file) as f:\n        content = f.readlines()\n        for line in content:\n            if line_to_find in line:\n                return True\n    return False\n\n\n# will read a file and look for a string. If the string exists, will return the whole line which comprise it\ndef get_line_from_file(file, str_to_find):\n    with open(file) as read_file:\n        for line in read_file:\n            if str_to_find in line:\n                return line\n    return None\n\n\n# will turn a json file to a dictionary\ndef json_file_to_dict(json_file):\n    import json\n\n    with open(json_file) as f:\n        data = json.load(f)\n    return data\n\n\n# will turn a dictionary to a json file\ndef dict_to_json_file(json_file, dictt):\n    import json\n    with open(json_file, 'w') as f:\n        json.dump(dictt, f)\n\n\ndef remove_lines_from_file(file_path, lines_arr_to_remove=None, remove_from=None, remove_until=None):\n    \"\"\"\n    Will remove lines from a file if the lines contains certain strings\n    Args:\n        param file_path: the path to your file\n        param lines_arr_to_remove: (optional) an array of lines to search for (this function calls contains() and not == )\n        param remove_from: (optional) if you want to remove a range of lines, set here the first line in which to start the removal)\n        param remove_until: (optional) if you want to remove a range of lines, set here the last line in which to end the removal)\n    \"\"\"\n    if lines_arr_to_remove is None:\n        lines_arr_to_remove = []\n    import fileinput\n    import sys\n    on_remove_sequence = False\n    for line in fileinput.input(file_path, inplace=1):\n        line_to_remove_found = False\n        if remove_from is not None and remove_from in line:\n            on_remove_sequence = True\n\n        if remove_until is not None and remove_until in line:\n            on_remove_sequence = False\n            continue\n\n        if on_remove_sequence:\n            continue\n\n        if lines_arr_to_remove is not None:\n            for line_to_remove in lines_arr_to_remove:\n                if line_to_remove in line:\n                    line_to_remove_found = True\n                    break\n\n        if line_to_remove_found:\n            continue\n\n        sys.stdout.write(line)\n\n\ndef file_to_bytes(path, n=None):\n    \"\"\"\n    Will read a file to bytes\n    Args:\n      param path: the path to your file\n      param n: the bytes count to read (leave None to read the whole file)\n    \"\"\"\n    in_file = open(path, \"rb\")\n    data = in_file.read(n)\n    in_file.close()\n    return data\n\n\n# will extract a zip file to a destination\ndef extract_zip_file(zip_file_path, dst_path):\n    from zipfile import ZipFile\n    # Create a ZipFile Object and load sample.zip in it\n\n    with ZipFile(zip_file_path, 'r') as zipObj:\n        # Extract all the contents of zip file in current directory\n        zipObj.extractall(dst_path)\n```\n\nAnd more...\n\n## Links\n[GitHub - osapps](https://github.com/osfunapps)\n\n## Licence\nISC","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosfunapps%2Fos-file-handler-py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fosfunapps%2Fos-file-handler-py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosfunapps%2Fos-file-handler-py/lists"}