file_with.py 438 B

123456789101112131415161718192021
  1. f = open("io/data/file1")
  2. with f as f2:
  3. print(f2.read())
  4. # File should be closed
  5. try:
  6. f.read()
  7. except:
  8. # Note: CPython and us throw different exception trying to read from
  9. # close file.
  10. print("can't read file after with")
  11. # Regression test: test that exception in with initialization properly
  12. # thrown and doesn't crash.
  13. try:
  14. with open('__non_existent', 'r'):
  15. pass
  16. except OSError:
  17. print("OSError")