Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.122 X-Spam-Level: * X-Spam-Evidence: '*H*': 0.78; '*S*': 0.03; 'practice,': 0.07; 'subject:file': 0.07; 'specific.': 0.09; '(either': 0.16; 'bytes)': 0.16; 'codecs': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'in;': 0.16; 'magic': 0.16; 'subject:possible': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'work,': 0.20; 'fairly': 0.24; 'handling': 0.26; 'header:In-Reply- To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'open': 0.33; 'common': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'next': 0.36; 'subject:?': 0.36; 'nov': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'bad': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'skip:p 20': 0.39; 'manually': 0.60; 'most': 0.60; 'first': 0.61; 'more': 0.64; 'finally': 0.65; 'capable': 0.67; 'subject:Using': 0.84; 'subject:handle': 0.84; 'subject:try': 0.84; 'technically': 0.84; 'victor': 0.84; 'wanted,': 0.84; 'careful': 0.91; '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:date:message-id:subject:from:to :content-type; bh=GUCU3dB+ePgzMTFTJow8BEFHzouUE/1xQhXcjQmqGLY=; b=wwPDgQ/WfAGtc/iXwdzNW1oZDBiGlwQ/vNMG2ls6sO8jbM7wiOD8SAKGLZq9o2Kofo tVLwzEU1HLJ+PyR2cmidynuAwiYmn+VbQkt0fBfuyE3SGFvJLi1i5HMXaHIl9sxMgeTd CPyh/yE7IF31kL/s0QFfWpJPOAS6rDwOpzan9nQMuUQa0YtkHWG3T+UrUR6HYuecT4Op vEdS6ukyy8BVum+kTwdXWO3LEQ53iQyZT7l0i9J5yBmDYrI1WqagInfucQ8HVab2Wo2W NsbXWA+ZSx8uQ1p1G/K3YiofHRz3LVGrBMtq0E8fL48TnEfeVOwoe/iKooXSkiYXnGbO iWPQ== MIME-Version: 1.0 X-Received: by 10.68.25.229 with SMTP id f5mr9373927pbg.6.1384845771004; Mon, 18 Nov 2013 23:22:51 -0800 (PST) In-Reply-To: <8379f7c2-c248-4a67-82ed-2d288a1635d2@googlegroups.com> References: <8379f7c2-c248-4a67-82ed-2d288a1635d2@googlegroups.com> Date: Tue, 19 Nov 2013 18:22:50 +1100 Subject: Re: Using try-catch to handle multiple possible file types? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 17 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384845774 news.xs4all.nl 15882 [2001:888:2000:d::a6]:52114 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59961 On Tue, Nov 19, 2013 at 6:13 PM, Victor Hooi wrote: > 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? It's fairly common to work that way. But you may want to be careful what order you try them in; some codecs might be technically capable of reading other formats than you wanted, so start with the most specific. Alternatively, looking at a file's magic number (either with python-magic/libmagic or by manually reading in a few bytes) might be more efficient. Either way can work, take your choice! ChrisA