python-有没有办法关闭PdfFileReader打开的文件?
作者:互联网
我正在打开许多PDF,并且要在解析它们之后删除它们,但是在程序运行完成之前,文件一直保持打开状态.如何关闭使用PyPDF2打开的PDf?
码:
def getPDFContent(path):
content = ""
# Load PDF into pyPDF
pdf = PyPDF2.PdfFileReader(file(path, "rb"))
#Check for number of pages, prevents out of bounds errors
max = 0
if pdf.numPages > 3:
max = 3
else:
max = (pdf.numPages - 1)
# Iterate pages
for i in range(0, max):
# Extract text from page and add to content
content += pdf.getPage(i).extractText() + "\n"
# Collapse whitespace
content = " ".join(content.replace(u"\xa0", " ").strip().split())
#pdf.close()
return content
解决方法:
只是自己打开和关闭文件
f = open(path, "rb")
pdf = PyPDF2.PdfFileReader(f)
f.close()
PyPDF2 .read()直接在构造函数中传递您传入的流.因此,在完成初始对象构建后,您只需扔掉文件即可.
上下文管理器也可以工作:
with open(path, "rb") as f:
pdf = PyPDF2.PdfFileReader(f)
do_other_stuff_with_pdf(pdf)
标签:python-2-7,pypdf2,python 来源: https://codeday.me/bug/20191025/1929752.html