problem with closing python pypdf - writing. getting a valueError: I/O operation on closed file -
can't figure function (part of class scraping internet site pdf) supposed merge pdf file generated web pages using pypdf.
this method code:
def mergepdf(self,mainname,inputlist=0): """merging pdf pages getting inputlist merge or defaults class instance self.pdftomerge list""" pypdf import pdffilewriter, pdffilereader self._mergelist = inputlist or self.pdftomerge self.pdfoutput = pdffilewriter() name in self._mergelist: print "merging %s main pdf file: %s" % (name,mainname) self._filestream = file(name,"rb") self.pdfinput = pdffilereader(self._filestream) p in self.pdfinput.pages: self.pdfoutput.addpage(p) self._filestream.close() self._pdfstream = file(mainname,"wb") self._pdfstream.open() self.pdfoutput.write(self._pdfstream) self._pdfstream.close()
i keep getting error:
file "c:\tmp\easy_install-iik9vj\pypdf-1.13-py2.7-win32.egg.tmp\pypdf\pdf.py", line 264, in write self._sweepindirectreferences(externalreferencemap, self._root) file "c:\tmp\easy_install-iik9vj\pypdf-1.13-py2.7-win32.egg.tmp\pypdf\pdf.py", line 339, in _sweepindirectreferences self._sweepindirectreferences(externmap, realdata) file "c:\tmp\easy_install-iik9vj\pypdf-1.13-py2.7-win32.egg.tmp\pypdf\pdf.py", line 315, in _sweepindirectreferences value = self._sweepindirectreferences(externmap, value) file "c:\tmp\easy_install-iik9vj\pypdf-1.13-py2.7-win32.egg.tmp\pypdf\pdf.py", line 339, in _sweepindirectreferences self._sweepindirectreferences(externmap, realdata) file "c:\tmp\easy_install-iik9vj\pypdf-1.13-py2.7-win32.egg.tmp\pypdf\pdf.py", line 315, in _sweepindirectreferences value = self._sweepindirectreferences(externmap, value) file "c:\tmp\easy_install-iik9vj\pypdf-1.13-py2.7-win32.egg.tmp\pypdf\pdf.py", line 324, in _sweepindirectreferences value = self._sweepindirectreferences(externmap, data[i]) file "c:\tmp\easy_install-iik9vj\pypdf-1.13-py2.7-win32.egg.tmp\pypdf\pdf.py", line 339, in _sweepindirectreferences self._sweepindirectreferences(externmap, realdata) file "c:\tmp\easy_install-iik9vj\pypdf-1.13-py2.7-win32.egg.tmp\pypdf\pdf.py", line 315, in _sweepindirectreferences value = self._sweepindirectreferences(externmap, value) file "c:\tmp\easy_install-iik9vj\pypdf-1.13-py2.7-win32.egg.tmp\pypdf\pdf.py", line 345, in _sweepindirectreferences newobj = data.pdf.getobject(data) file "c:\tmp\easy_install-iik9vj\pypdf-1.13-py2.7-win32.egg.tmp\pypdf\pdf.py", line 645, in getobject self.stream.seek(start, 0) valueerror: i/o operation on closed file
but when check status of self._pdfstream get:
<open file 'c:\python27\learn\dive.pdf', mode 'wb' @ 0x013b2020>
what doing wrong?
i'll glad help
ok, found problem. right call file()
. don't try call open()
@ all.
your problem input file still needs open when call self.pdfoutput.write(self._pdfstream)
, need remove line self._filestream.close()
.
edit: script trigger problem. first write succeed , second fail.
from pypdf import pdffilereader pfr, pdffilewriter pfw input_filename = 'in.pdf' # replace real file output_filename = 'out.pdf' # 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 ioerror before line" outfile.close()
Comments
Post a Comment