csv.reader()
doesn’t require a file object, nor does open()
. Does pyPdf2.PdfFileReader()
require a file object because of the complexity of the PDF format, or is there some other reason?
Advertisement
Answer
It’s just a matter of how the library was written. csv.reader
allows any iterable that returns strings (which includes files). open
is opening the file, so of course it doesn’t take an open file (although it can take an integer pointing at an open file descriptor). Typically, it is better to handle the file separately, usually within a with
block so that it is closed properly.
with open('input.pdf', 'rb') as f: # do something with the file