I have some code to open and search through a folder full of pdfs. I'm using pdfminer to do the pdf conversion. But, some of my pdfs are not readable. I want my code to process those pdfs where the conversion works, and effectively skip over those pdfs where the conversion fails.
I'm trying to use the try/except feature, but it doesn't seem to be working. For the pdfs that fail, the exception works. But, for the pdfs where the conversion works, both the try and exception blocks are executed.
Here's my code:
fileNum = 0
d = shelve.open('PyDocSearch.db')
for file in fileList:
fileNum += 1
z = []
try:
doc = convert_pdf(filePath + '/' + file)
print 'Success:',file
docWords = doc.split()
x = Counter(docWords)
y = x.most_common()
for i,j in enumerate(y):
if j[0] not in commonWords:
z.append(j)
d[file] = z
except:
doc = 'fail'
print 'Fail:',file
d[file] = doc
d.close()
When the pdf conversion works, why are both blocks being executed? And, how can I prevent this from happening? Thanks!
exceptblock, but specify what kinds of exceptions you want to catch. Then you'll narrow down the cases where theexceptis executed.tryas you are actually testing for exceptions. Move everything else below theexcept.