Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3.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.023 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'detect': 0.07; 'practice,': 0.07; 'subject:file': 0.07; 'assuming': 0.09; 'type,': 0.09; 'cc:addr:python-list': 0.11; 'constructs': 0.16; 'keyed': 0.16; 'subject:possible': 0.16; 'thoughts?': 0.16; 'wraps': 0.16; 'exception': 0.16; 'files.': 0.16; 'wrote:': 0.18; 'input': 0.22; 'saying': 0.22; 'cc:addr:python.org': 0.22; 'instead.': 0.24; 'file.': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'script': 0.25; 'handling': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; 'etc.).': 0.31; 'object.': 0.31; 'file': 0.32; 'open': 0.33; 'could': 0.34; 'common': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'next': 0.36; 'subject:?': 0.36; 'hi,': 0.36; 'should': 0.36; 'starting': 0.37; 'ahead': 0.38; 'nov': 0.38; 'handle': 0.38; 'files': 0.38; 'pm,': 0.38; 'does': 0.39; 'bad': 0.39; 'how': 0.40; 'above,': 0.60; 'most': 0.60; 'first': 0.61; 'different': 0.65; 'finally': 0.65; 'to:addr:gmail.com': 0.65; 'url:me': 0.69; 'subject:Using': 0.84; 'subject:handle': 0.84; 'subject:try': 0.84; 'victor': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=soVponpbvWMmoCrQySIrC3h1A1krN1SjfOJjUyBfYpE=; b=tPh8rrZgcZhwf9/VINeDXmXFyo0NQtIVuntPz9fweJv+XBZTI76lercO137Xbk5NHR Dz+CuvxrxtF2QF+1VHHhJh8smZM2fwauNvHKVUAARuxyRaz0KEiC6M55OEuN7Lo5Kjn7 5NFbONnZHi+Bz9Kc/Fc6QhWdl1eWzaZtRvfaZiTtZKTdeBYuXWHMVCKLDVIqHeiandOf Lysu7Z1gK/SlQ9kJdVAAyZkGwTjqH/Mvkn9MuvesOj6wbRBM5p6bovEVdcjIln4xxK5C c9ni79KIahyuzO/wVyF/tQiPEsFUAgBw9QNy2AP1btjjIXAvjkX2I7vw0snrxx1fU1mt UdSg== X-Received: by 10.112.236.103 with SMTP id ut7mr55141lbc.39.1384845773564; Mon, 18 Nov 2013 23:22:53 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <8379f7c2-c248-4a67-82ed-2d288a1635d2@googlegroups.com> References: <8379f7c2-c248-4a67-82ed-2d288a1635d2@googlegroups.com> From: Amit Saha Date: Tue, 19 Nov 2013 17:22:13 +1000 Subject: Re: Using try-catch to handle multiple possible file types? To: Victor Hooi Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org 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: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384846203 news.xs4all.nl 15981 [2001:888:2000:d::a6]:54717 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59962 On Tue, Nov 19, 2013 at 5:13 PM, Victor Hooi wrote: > Hi, > > I have a script that needs to handle input files of different types (uncompressed, gzipped etc.). > > My question is regarding how I should handle the different cases. > > My first thought was to use a try-catch block and attempt to open it using the most common filetype, then if that failed, try the next most common type etc. before finally erroring out. > > So basically, using exception handling for flow-control. > > However, is that considered bad practice, or un-Pythonic? > > What other alternative constructs could I also use, and pros and cons? > > (I was thinking I could also use python-magic which wraps libmagic, or I can just rely on file extensions). > > Other thoughts? How about starting with a dictionary like this: file_opener = {'.gz': gz_opener, '.txt': text_opener, '.zip': zip_opener} # and so on. where the *_opener are say functions which does the job of actually opening the files. The above dictionary is keyed on file extensions, but perhaps you would be better off using MIME types instead. Assuming you go ahead with using MIME type, how about using python-magic to detect the type and then look in your dictionary above, if there is a corresponding file_opener object. If you get a KeyError, you can raise an exception saying that you cannot handle this file. How does that sound? Best, Amit. -- http://echorand.me