Christian
Christian

Reputation: 25

How do you close this in Python?

I need to be able to close the PDF but I haven't been able to figure out how in PyPDF2 using Python 2.7

I open it below but then I try the .close():

input1 = PdfFileReader(open(var_loopingpath+f,"rb"))

And get the error:

'PdfFileReader' object has no attribute 'close'

Thanks.

Upvotes: 0

Views: 1656

Answers (1)

DeepSpace
DeepSpace

Reputation: 81664

Use with and don't bother with closing the file:

with open(var_loopingpath+f,"rb") as pdf_file:
    input1 = PdfFileReader(pdf_file)
    # rest of code

This way Python will close pdf_file when it goes out of scope.

Upvotes: 3

Related Questions