May-29-2019, 07:22 AM
(This post was last modified: May-29-2019, 07:22 AM by Gribouillis.)
See also contextlib.contextmanager for a simplified interface and also contextlib.closing and the like
import contextlib @contextlib.contextmanager def my_open(*args, **kwargs): fd = os.open(*args, **kwargs) try: yield fd finally: os.close(fd) with my_open(my_thing) as desc: ...