1

Possible Duplicate:
How to close pyPDF “PdfFileReader” Class file handle

I'm running Python 2.7 on Win 64bit. I'm trying to download a pdf to file, open the pdf, extract the text, and then at the end delete the pdf, but getting an error when I try to delete the python (using the python code and manually trying to delete the pdf - windows says the file is in use by pythonw). My simplified code:

from PyPDF2 import PdfFileReader

pdf_url = "www.Url for.pdf"

file_path = 'myfile.pdf'
f = file(file_path, 'wb')
f.write(urllib2.urlopen(pdf_url).read())
f.close()

pdf = PdfFileReader(open(file_path, 'rb'))

os.remove(file_path)

Error returned:

WindowsError: [Error 32] The process cannot access the file because it is 
being used by another process: 'myfile.pdf'

Any suggestions?

Community
  • 1
  • 1
Daniel
  • 43
  • 5
  • 1
    Saw that before, didn't work though. The OP's solution was to add "fd.close()" which I have already done. Solution 1 was to add "reader.stream.close()" but I got "NameError: global name 'reader' is not defined" back. Solution 2 gave me "PdfReadError: EOF marker not found" – Daniel Oct 26 '12 at 08:47
  • In this example you could use `StringIO` instead of creating the file in the first place. – Janne Karila Oct 26 '12 at 08:56

0 Answers0