Problem With Closing Python Pypdf - Writing. Getting A ValueError: I/O Operation On Closed File
can't figure this up this function (part of class for scraping internet site into a pdf) supposed to merge the pdf file generated from web pages using pypdf. this is the method co
Solution 1:
OK, I found your problem. You were right to call file()
. Don't try to call open()
at all.
Your problem is the input file still needs to be open when you call self.pdfoutput.write(self._pdfstream)
, so you need to remove the line self._filestream.close()
.
Edit: This script will trigger the problem. The first write will succeed and the second will fail.
from pyPdf import PdfFileReader as PfR, PdfFileWriter as PfW
input_filename = 'in.PDF' # replace with a real file
output_filename = 'out.PDF' # something that doesn't exist
infile = file(input_filename, 'rb')
reader = PfR(infile)
writer = PfW()
writer.addPage(reader.getPage(0))
outfile = file(output_filename, 'wb')
writer.write(outfile)
print "First Write Successful!"
infile.close()
outfile.close()
infile = file(input_filename, 'rb')
reader = PfR(infile)
writer = PfW()
writer.addPage(reader.getPage(0))
outfile = file(output_filename, 'wb')
infile.close() # BAD!
writer.write(outfile)
print "You'll get an IOError Before this line"
outfile.close()
Post a Comment for "Problem With Closing Python Pypdf - Writing. Getting A ValueError: I/O Operation On Closed File"