On 17/01/2014 05:42, vasishtha.sp...@gmail.com wrote: > try: > n = 0 > for dirpath, dirnames, filenames in os.walk(root): > PdfOutputFileName = os.path.basename(dirpath) + ".pdf" > c = canvas.Canvas(PdfOutputFileName) > if n > 0 : > for filename in filenames: > LowerCaseFileName = filename.lower() > if LowerCaseFileName.endswith(".jpg"): > print(filename) > filepath = os.path.join(dirpath, filename) > print(filepath) > im = ImageReader(filepath) > imagesize = im.getSize() > c.setPageSize(imagesize) > c.drawImage(filepath,0,0) > c.showPage() > c.save() > n = n + 1 > print "PDF of Image directory created" + PdfOutputFileName > > except: > print "Failed creating PDF" > -------------------------
One thing I would point out (assuming that this is your final code): your try-except is too broad, both in terms of the code it encloses and in terms of the exceptions it traps. As it stands, your code will drop straight out as soon as it hits an error, with the message "Failed creating PDF" -- which is what it would have done anyway, only you've removed the informative traceback which would have told you what went wrong! In the circumstances, you presumably want to attempt to recover from some failure (perhaps caused by a corrupt JPEG or a permissions issue) and continue to generate the remaning PDFs. In that case, you'd do better a structure of this sort: <semi-pseudocode> import logging logging.basicConfig() for d, ds, fs in os.walk("..."): # init pdf try: # create PDF except: logging.exception("Couldn't create PDF for %s", d) continue else: logging.info("Created PDF for %s", d) # write PDF </semi-pseudocode> If you could narrow down the range of exceptions you want to recover from, that would go in the "except:" clause, but in this situation you might not be in a position to do that. TJG -- https://mail.python.org/mailman/listinfo/python-list