I am building a program that verifies media and doc files within a given file tree. When it tries to read a pdf (with PyPDF2), it will occasionally freeze the program. I want to implement a timeout on the function so that it just times out and logs an error rather than crashing. Here is my code for the PDF function:
def pdf_verify(file, good_files, bad_files):
try:
PyPDF2.PdfFileReader(open(file, "rb"))
good_files.append(file)
except:
bad_files.append(file)
return good_files, bad_files
It tries to open the PDF file with PyPDF2, and if it can't be opened, it catches the exception and appends the file to a bad file list. I know there is a way to do this on Linux, but I need the program to be runnable on Windows. Is there any way to do this? Thanks!