I have a use-case that resembles the following:
files = [open("foo1.pdf", "rb"), open("foo2.pdf", "rb"), open("foo3.pdf", "rb")]
# ... extract portions from the opened files using PyPDF2 and assemble a new PDF file
map(close, files)
Why I do that above? because while using PyPDF2 to merge multiple input PDF files into another file, if you close each input PDF you get empty pages in the output PDF. The input files have to stay open until the output PDF file is generated see https://github.com/mstamy2/PyPDF2/issues/293
Results in the following error:
NameError: name 'close' is not defined
The following works but I'd like the less verbose code variation:
map(lambda file: file.close(), files)
I'd of course prefer the following instead:
map(close, files)
closemethod, but some non-existingclosefunction that would be used likeclose(f). - Thierry Lathuillecontextlib.ExitStackwhich closes the files automatically for you. - a_guest