Answers for "python context manager example"

1

python context manager example

class File:
  def __init__(self, filename, method):
    self.file = open(filename, method)
   
  def __enter__(self):
    print("Enter")
    return self.file
  
  def __exit__(self, type, value, traceback):
    print(f"{type}, {value}, {traceback}")
    print("Exit")
    self.file.close()

with File("file.txt", "w") as f:
  print("Middle")
  f.write("hello")
  raise Exception()
  raise FileExistsEoor()
Posted by: Guest on May-13-2021

Code answers related to "python context manager example"

Python Answers by Framework

Browse Popular Code Answers by Language