{"id":21997674,"url":"https://github.com/osfunapps/os-file-stream-handler-py","last_synced_at":"2025-10-15T03:24:18.604Z","repository":{"id":57449719,"uuid":"307102163","full_name":"osfunapps/os-file-stream-handler-py","owner":"osfunapps","description":"this module contains file stream handling functions to implement in a Python project.","archived":false,"fork":false,"pushed_at":"2021-02-07T14:01:56.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-28T11:32:14.548Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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-25T13:13:19.000Z","updated_at":"2021-02-07T14:01:58.000Z","dependencies_parsed_at":"2022-09-26T17:31:09.360Z","dependency_job_id":null,"html_url":"https://github.com/osfunapps/os-file-stream-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-stream-handler-py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osfunapps%2Fos-file-stream-handler-py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osfunapps%2Fos-file-stream-handler-py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osfunapps%2Fos-file-stream-handler-py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/osfunapps","download_url":"https://codeload.github.com/osfunapps/os-file-stream-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:38.997Z","updated_at":"2025-10-15T03:24:13.573Z","avatar_url":"https://github.com/osfunapps.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"Introduction\n------------\n\nThis module contains file stream handling functions to implement in a Python project.\n\n## Installation\nInstall via pip:\n\n    pip install os-file-stream-handler\n\n\n## Usage       \n```python        \nfrom file_stream_handler import file_stream_handler\n```\n\n## Functions and signatures:\n```python\n\n# will read a file from a path\ndef read_text_file(file_path, drop_new_lines=False):\n    with open(file_path, 'r') as fd:\n        reader = fd.readlines()\n    lines = list(reader)\n    if drop_new_lines:\n        for i in range(0, len(lines)):\n            lines[i] = lines[i].replace('\\n', '')\n    return lines\n\n\n# will read a binary file (usually file which contains bytes)\ndef read_binary_file(file_path, bytes_count=None):\n    in_file = open(file_path, \"rb\")\n    if bytes_count:\n        data = in_file.read(bytes_count)\n    else:\n        data = in_file.read()\n    in_file.close()\n    return data\n\n\n# for text files you should look for the right initials\ndef compress_data_to_file(dst_file, data, write_type='wb'):\n    import gzip\n\n    with gzip.open(dst_file, write_type) as f:\n        f.write(data)\n\n\ndef compress_text_file_to_file(src_file, dst_file):\n    data = \"\".join(read_text_file(src_file)).encode(\"utf8\")\n    compress_data_to_file(dst_file, data, write_type='wb')\n\n\n# will read a compressed file. rb stands for 'read binary'\n# for text files you should look for the right initials\ndef read_compressed_file(file_path, read_type='rb'):\n    import gzip\n\n    with gzip.open(file_path, read_type) as f:\n        return f.read()\n\n\n# will write a content to a file (str or array of strings)\ndef write_file(file_path, content):\n    with open(file_path, 'w') as f:\n        from os_file_handler import file_handler as fh\n        parent_dir = fh.get_parent_path(file_path)\n        if not fh.is_dir_exists(parent_dir):\n            fh.create_dir(parent_dir)\n\n        if isinstance(content, str):\n            f.write(content)\n        if isinstance(content, list):\n            for line in content:\n                if not str(line).endswith('\\n'):\n                    line += '\\n'\n                f.write(line)\n\n\n# will replace a bunch of chars in a file\ndef replace_text_in_file(file_src, file_dst, old_expression, new_expression, replace_whole_line=False, cancel_if_exists=False):\n    lines = read_text_file(file_src, drop_new_lines=True)\n\n    if cancel_if_exists and is_line_exists_in_text(new_expression, lines=lines):\n        return\n    with open(file_dst, 'w') as f:\n        from os_file_handler import file_handler as fh\n        parent_dir = fh.get_parent_path(file_dst)\n        if not fh.is_dir_exists(parent_dir):\n            fh.create_dir(parent_dir)\n\n        for line in lines:\n\n            if old_expression in line:\n                if replace_whole_line:\n                    if new_expression == '':\n                        continue\n                    else:\n                        line = new_expression\n                else:\n                    line = line.replace(old_expression, new_expression)\n            f.write(f'{line}\\n')\n\n\n# will delete a line contains an expression from a text\ndef delete_line_in_file(file_src, file_dst, expression):\n    replace_text_in_file(file_src, file_src, expression, '', True)\n\n\n# will add text below some other text in a file\ndef append_text_below_line_in_file(file_src, file_dst, below_line, new_expression, cancel_if_exists=False):\n    lines = read_text_file(file_src, drop_new_lines=True)\n\n    if cancel_if_exists and is_line_exists_in_text(new_expression, lines=lines):\n        return\n    with open(file_dst, 'w') as f:\n        from os_file_handler import file_handler as fh\n        parent_dir = fh.get_parent_path(file_dst)\n        if not fh.is_dir_exists(parent_dir):\n            fh.create_dir(parent_dir)\n\n        for i in range(0, len(lines)):\n            f.write(f'{lines[i]}\\n')\n            if below_line in lines[i]:\n                f.write(f'{new_expression}\\n')\n\n\n# will add text above some other text in a file\ndef append_text_above_line_in_file(file_src, file_dst, above_line, new_expression, cancel_if_exists=False):\n    lines = read_text_file(file_src, drop_new_lines=True)\n    if cancel_if_exists and is_line_exists_in_text(new_expression, lines=lines):\n        return\n\n    with open(file_dst, 'w') as f:\n        from os_file_handler import file_handler as fh\n        parent_dir = fh.get_parent_path(file_dst)\n        if not fh.is_dir_exists(parent_dir):\n            fh.create_dir(parent_dir)\n\n        for i in range(0, len(lines)):\n            if above_line in lines[i]:\n                f.write(f'{new_expression}\\n')\n            f.write(f'{lines[i]}\\n')\n\n\n# will check if line exists in a file\ndef is_line_exists_in_text(line_to_find, file_src=None, lines=None):\n    if file_src:\n        lines = read_text_file(file_src)\n    for line in lines:\n        if line_to_find in line:\n            return True\n    return False\n\n\n# will delete a text in a range\ndef delete_text_range_in_file(file_src, file_dst, from_text, to_text, include_bundaries=False):\n    lines = read_text_file(file_src, drop_new_lines=True)\n\n    with open(file_dst, 'w') as f:\n        from os_file_handler import file_handler as fh\n        parent_dir = fh.get_parent_path(file_dst)\n        if not fh.is_dir_exists(parent_dir):\n            fh.create_dir(parent_dir)\n\n        from_text_found = False\n        done = False\n        for i in range(0, len(lines)):\n            if done:\n                f.write(f'{lines[i]}\\n')\n                continue\n            if from_text in lines[i]:\n                from_text_found = True\n                if include_bundaries:\n                    f.write(f'{lines[i]}\\n')\n            if from_text_found and to_text in lines[i]:\n                done = True\n                if include_bundaries:\n                    f.write(f'{lines[i]}\\n')\n            if not from_text_found:\n                f.write(f'{lines[i]}\\n')\n\n\ndef clear_text_from_last(lines, text):\n    for i in reversed(range(0, len(lines))):\n        found = False\n        if text in lines[i]:\n            found = True\n        lines.pop(-1)\n        if found:\n            return lines\n```\n\nAnd more...\n\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-stream-handler-py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fosfunapps%2Fos-file-stream-handler-py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosfunapps%2Fos-file-stream-handler-py/lists"}