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 | |
First commit, add all files
Diffstat (limited to 'py04')
| -rwxr-xr-x | py04/ex0/ft_ancient_text.py | 44 | ||||
| -rwxr-xr-x | py04/ex1/ft_archive_creation.py | 96 | ||||
| -rwxr-xr-x | py04/ex2/ft_stream_management.py | 102 | ||||
| -rwxr-xr-x | py04/ex3/ft_vault_security.py | 48 |
4 files changed, 290 insertions, 0 deletions
diff --git a/py04/ex0/ft_ancient_text.py b/py04/ex0/ft_ancient_text.py new file mode 100755 index 0000000..152c0a2 --- /dev/null +++ b/py04/ex0/ft_ancient_text.py @@ -0,0 +1,44 @@ +#!/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("Usage: ft_ancient_text.py <file>") + except FileNotFoundError as e: + print(f"Error opening the file '{sys.argv[1]}':", e) + # trying to write when disk is full + # also catch permission errors + except OSError as e: + print(e) + # No permission to read or write + except PermissionError as e: + print(e) + # file encoded in a format other than UTF-8 + except UnicodeDecodeError as e: + print(e) + except IsADirectoryError as e: + print(e) + finally: + if file: + file.close() + print("---") + print(f"File '{sys.argv[1]}' closed.") + + +def main() -> None: + print("=== Cyber Archives Recovery ===") + read_content() + + +if __name__ == "__main__": + main() diff --git a/py04/ex1/ft_archive_creation.py b/py04/ex1/ft_archive_creation.py new file mode 100755 index 0000000..2ae1612 --- /dev/null +++ b/py04/ex1/ft_archive_creation.py @@ -0,0 +1,96 @@ +#!/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("Usage: ft_ancient_text.py <file>") + except FileNotFoundError as e: + print(f"Error opening the file '{sys.argv[1]}':", e) + # trying to write when disk is full + # also catch permission errors + except OSError as e: + print(e) + # No permission to read or write + except PermissionError as e: + print(e) + # file encoded in a format other than UTF-8 + except UnicodeDecodeError as e: + print(e) + except IsADirectoryError as e: + print(e) + finally: + if file: + file.close() + print("---") + print(f"File '{sys.argv[1]}' closed.") + + +def new_perservation_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() + new_file_name = input("Enter a new file name (or empty): ") + 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("No file to copy") + except FileNotFoundError as e: + print(f"Error opening the file '{sys.argv[1]}':", e) + # trying to write when disk is full + # also catch permission errors + except OSError as e: + print(e) + # No permission to read or write + except PermissionError as e: + print(e) + # file encoded in a format other than UTF-8 + except UnicodeDecodeError as e: + print(e) + except IsADirectoryError as e: + print(e) + 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_perservation_protocol() + + +if __name__ == "__main__": + main() 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() diff --git a/py04/ex3/ft_vault_security.py b/py04/ex3/ft_vault_security.py new file mode 100755 index 0000000..3395a38 --- /dev/null +++ b/py04/ex3/ft_vault_security.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +# Create a function secure_archive() that provides safe access to any file for +# reading or writing. It returns a tuple (True|False, str) that indicates +# whether the operation #succeeded (the boolean) and provides the associated +# content (either the file’s contents or an error message). The function takes +# the following parameters: a mandatory file name, an optional int or str +# (your choice) that indicates the action to perform (read or write), and +# another optional string that contains the content to write to the file. +# https://docs.python.org/3/library/functions.html#repr + +def secure_archive(file: str, write=None, to_write=None) -> tuple[bool, str]: + try: + with open(file, "r") as f: + content = f.read() + if write is None: + return (True, content) + if write: + with open(file, "w") as f: + f.write(to_write) + return (True, to_write) + except FileNotFoundError as err: + content = f"{err}" + except PermissionError as err: + content = f"{err}" + except OSError as err: + content = f"{err}" + return (False, content) + + +def main() -> None: + to_write = "Content successfully write to file" + print("=== Cyber Archives Security ===") + print("Using 'secure archive' to read from a non-existent file:") + print(secure_archive("file_does_not_exist.txt")) + print() + print("Using 'secure archive' to read from accessible file:") + print(secure_archive("permission_issue.txt")) + print() + print("Using 'secure archive' to read a regular file:") + print(secure_archive("ancient_fragment.txt")) + print() + print("Using 'secure archive' to write content to file :") + # pass 1 to write + print(secure_archive("ancient_fragment.txt", 1, to_write)) + + +if __name__ == "__main__": + main() |
