title: problem with closing python pypdf - writing. getting a valueError: I/O operation on closed file
body: <p>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.</p>
  
  <p>this is the method code:</p>
  
  <pre><code>def mergePdf(self,mainname,inputlist=0):
      """merging the pdf pages
      getting an inputlist to merge or defaults to the class instance self.pdftomerge list"""
      from pyPdf import PdfFileWriter, PdfFileReader
      self._mergelist = inputlist or self.pdftomerge
      self.pdfoutput = PdfFileWriter()
  
      for name in self._mergelist:
          print "merging %s into main pdf file: %s" % (name,mainname)
          self._filestream = file(name,"rb")
          self.pdfinput = PdfFileReader(self._filestream)
          for 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()
  </code></pre>
  
  <p>I keep getting this error:</p>
  
  <pre><code>  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
  </code></pre>
  
  <p>but when I check the status of self._pdfstream I get:</p>
  
  <pre><code>&lt;open file 'c:\python27\learn\dive.pdf', mode 'wb' at 0x013B2020&gt;
  </code></pre>
  
  <p>what am I doing wrong?</p>
  
  <p>i'll be glad for any help</p>