diff options
| author | yctct <yctct@yctct.com> | 2026-06-07 08:59:04 +0200 |
|---|---|---|
| committer | yctct <yctct@yctct.com> | 2026-06-07 08:59:04 +0200 |
| commit | 15115b4c52bfda0d1cca9fa1155beecbb873ec35 (patch) | |
| tree | b3f0975e63eb04dcba732a78ce9bd9abda8acf01 /py04/ex2 | |
First commit, add all files
Diffstat (limited to 'py04/ex2')
| -rwxr-xr-x | py04/ex2/ft_stream_management.py | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/py04/ex2/ft_stream_management.py b/py04/ex2/ft_stream_management.py new file mode 100755 index 0000000..4c2544b --- /dev/null +++ b/py04/ex2/ft_stream_management.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python3 + +import sys + + +def read_content() -> None: + file = None + try: + file = open(sys.argv[1]) + content = file.read() + print(f"Accessing file {sys.argv[1]}") + print("---") + print() + print(content) + except IndexError: + print("[STDERR] Usage: ft_ancient_text.py <file>", file=sys.stderr) + except FileNotFoundError as e: + print(f"[STDERR] Error opening the file '{sys.argv[1]}':", + e, file=sys.stderr) + # trying to write when disk is full + # also catch permission errors + except OSError as e: + print("[STDERR]", e, file=sys.stderr) + # No permission to read or write + except PermissionError as e: + print("[STDERR]", e, file=sys.stderr) + # file encoded in a format other than UTF-8 + except UnicodeDecodeError as e: + print("[STDERR]", e, file=sys.stderr) + except IsADirectoryError as e: + print("[STDERR]", e, file=sys.stderr) + finally: + if file: + file.close() + print("---") + print(f"File '{sys.argv[1]}' closed.") + + +def new_preservation_protocol() -> None: + # give files a value so I can close them anyway + original_file = None + new_file = None + # create a list to store modified lines + data = [] + try: + original_file = open(sys.argv[1], 'r') + print("Transform data") + print("---") + for line in original_file: + line = line.rstrip('\n') + line = line + "#" + data.append(line) + print(line) + original_file.close() + sys.stdout.write("Enter a new file name (or empty): ") + # flush buffer + sys.stdout.flush() + new_file_name = sys.stdin.readline() + new_file_name = new_file_name.rstrip('\n') + if not new_file_name: + print("Not saving data") + else: + print(f"Saving data to '{new_file_name}'") + new_file = open(new_file_name, 'w') + for line in data: + new_file.write(line) + new_file.write('\n') + print(f"Date saved in file '{new_file_name}'") + new_file.close() + except IndexError: + print("[STDERR] No file to copy", file=sys.stderr) + except FileNotFoundError as e: + print(f"[STDERR] Error opening the file '{sys.argv[1]}':", + e, file=sys.stderr) + # trying to write when disk is full + # also catch permission errors + except OSError as e: + print("[STDERR]", e, file=sys.stderr) + # No permission to read or write + except PermissionError as e: + print("[STDERR]", e, file=sys.stderr) + # file encoded in a format other than UTF-8 + except UnicodeDecodeError as e: + print("[STDERR]", e, file=sys.stderr) + except IsADirectoryError as e: + print("[STDERR]", e, file=sys.stderr) + finally: + if original_file: + original_file.close() + if new_file: + new_file.close() + + +def main() -> None: + print("=== Cyber Archives Recovery & Preservation ===") + read_content() + print() + new_preservation_protocol() + + +if __name__ == "__main__": + main() |
