【发布时间】:2017-10-12 21:14:00
【问题描述】:
我正在检查文件系统中损坏的 PDF。在我正在运行的测试中,有将近 200k PDF。似乎较小的损坏文件正确警报,但我遇到了一个已损坏的 15 MB 大文件,代码只是无限期挂起。我尝试将 Strict 设置为 False,但没有运气。似乎问题在于最初的开放。而不是做线程和设置超时(我过去尝试过但收效甚微),我希望有一个替代方案。
import PyPDF2, os
from time import gmtime,strftime
path = raw_input("Enter folder path of PDF files:")
t = open(r'c:\pdf_check\log.txt','w')
count = 1
for dirpath,dnames,fnames in os.walk(path):
for file in fnames:
print count
count = count + 1
if file.endswith(".pdf"):
file = os.path.join(dirpath, file)
try:
PyPDF2.PdfFileReader(file,'rb',warndest="c:\test\warning.txt")
except PyPDF2.utils.PdfReadError:
curdate = strftime("%Y-%m-%d %H:%M:%S", gmtime())
t.write(str(curdate) + " " + "-" + " " + file + " " + "-" + " " + "fail" + "\n")
else:
pass
#curdate = strftime("%Y-%m-%d %H:%M:%S", gmtime())
#t.write(str(curdate) + " " + "-" + " " + file + " " + "-" + " " + "pass" + "\n")
t.close()
【问题讨论】: