Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'init': 0.07; '%s",': 0.09; 'except:': 0.09; 'filename': 0.09; 'filenames': 0.09; 'filenames:': 0.09; 'permissions': 0.09; 'try:': 0.09; '(assuming': 0.16; '05:42,': 0.16; 'clause,': 0.16; 'corrupt': 0.16; 'dirnames,': 0.16; 'dirpath,': 0.16; 'filename)': 0.16; 'from:addr:timgolden.me.uk': 0.16; 'from:name:tim golden': 0.16; 'hits': 0.16; 'message-id:@timgolden.me.uk': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'subject:Converting': 0.16; 'subject:folder': 0.16; 'tjg': 0.16; 'wrote:': 0.18; 'import': 0.22; 'print': 0.22; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'received:192.168.100': 0.24; 'skip:l 30': 0.24; 'logging': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'went': 0.31; 'code': 0.31; 'that.': 0.31; '(perhaps': 0.31; 'exceptions': 0.31; 'informative': 0.31; 'subject:per': 0.31; 'skip:c 30': 0.32; 'could': 0.34; 'case,': 0.35; 'but': 0.35; 'done': 0.36; 'too': 0.37; 'skip:- 20': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'pdf': 0.39; 'structure': 0.39; 'to:addr:python.org': 0.39; 'range': 0.61; "you've": 0.63; 'email addr:gmail.com': 0.63; 'soon': 0.63; 'situation': 0.65; 'caused': 0.69; 'from:addr:mail': 0.83; 'code):': 0.84; 'pdfs.': 0.84; 'presumably': 0.84; 'wrong!': 0.84; 'recover': 0.91 Date: Fri, 17 Jan 2014 09:01:52 +0000 From: Tim Golden User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Converting folders of jpegs to single pdf per folder References: <0c648d2d-d8b3-4848-8b37-1e5e1ae40327@googlegroups.com> <96cfad0d-143f-4084-827f-a67006bf6db2@googlegroups.com> In-Reply-To: <96cfad0d-143f-4084-827f-a67006bf6db2@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389949315 news.xs4all.nl 2975 [2001:888:2000:d::a6]:34407 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64146 On 17/01/2014 05:42, vasishtha.spier@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: 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 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