Yair Eshel wrote at 2024-6-13 13:01 +0300: > ... >I would like to suggest an alternative syntax, that will, in a sense, apply >the best of both worlds: > >import logging >with open('sample_data/README.md') as f: > print (len(f.read())) >except FileNotFoundError: > logging.error("File not")
Are you aware that in the case of a `FileNotFoundError` no context manager is created (the context manager is the `f` in your code). Why not use: try: with open()... ... except FileNotFoundError: ... I do not think that your use case requires a `with` extension. -- Dieter -- https://mail.python.org/mailman/listinfo/python-list