1

可能重复:
如何关闭 pyPDF “PdfFileReader” 类文件句柄

我在 Win 64bit 上运行 Python 2.7。我正在尝试将pdf下载到文件,打开pdf,提取文本,然后最后删除pdf,但是当我尝试删除python时出现错误(使用python代码并手动尝试删除pdf - windows 说该文件正在被 pythonw 使用)。我的简化代码:

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)

返回错误:

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

有什么建议么?

4

0 回答 0