1class File:
2 def __init__(self, filename, method):
3 self.file = open(filename, method)
4
5 def __enter__(self):
6 print("Enter")
7 return self.file
8
9 def __exit__(self, type, value, traceback):
10 print(f"{type}, {value}, {traceback}")
11 print("Exit")
12 self.file.close()
13
14with File("file.txt", "w") as f:
15 print("Middle")
16 f.write("hello")
17 raise Exception()
18 raise FileExistsEoor()