我正在检查文件系统中损坏的 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()