summaryrefslogtreecommitdiff
path: root/py04/ex0/ft_ancient_text.py
blob: 152c0a2f62d2bfd93a5aa2303cbcb425ed58f173 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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()