In brief
contextlib.ExitStack collects context managers and cleanup actions. It helps when input determines how many files need opening rather than the number being fixed in advance.
Useful to know
contextlib.ExitStack collects context managers and cleanup actions. It helps when input determines how many files need opening rather than the number being fixed in advance. [1]
Cleanup follows the registered context rules. It does not automatically roll back data already written as a transaction would. Distinguish closing resources from recovery after an operation has partly completed.
Sources
For further detail and to check the information: