Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'else:': 0.03; 'modified:': 0.05; 'context': 0.07; 'result,': 0.07; 'subject:file': 0.07; 'suppose': 0.07; 'string': 0.09; 'function:': 0.09; 'option,': 0.09; 'try:': 0.09; 'cc:addr:python- list': 0.11; 'def': 0.12; "'rb')": 0.16; '(everything': 0.16; 'code?': 0.16; 'ioerror': 0.16; 'ioerror:': 0.16; 'programmable': 0.16; 'stack:': 0.16; 'subject:possible': 0.16; 'types,': 0.16; 'unix,': 0.16; 'wrote:': 0.18; 'not,': 0.20; 'cc:addr:python.org': 0.22; 'case.': 0.24; 'helper': 0.24; 'guys': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'specifically': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'lines': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'way?': 0.31; 'file': 0.32; 'python.org': 0.32; 'another': 0.32; 'url:python': 0.33; 'cases': 0.33; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'data,': 0.36; 'url:listinfo': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'manager': 0.38; 'nov': 0.38; 'handle': 0.38; 'pm,': 0.38; 'sure': 0.39; 'enough': 0.39; 'either': 0.39; 'url:mail': 0.40; 'how': 0.40; 'read': 0.60; 'break': 0.61; 'offer': 0.62; 'more': 0.64; 'different': 0.65; 'needing': 0.65; 'hours': 0.66; 'opener': 0.84; 'subject:Using': 0.84; 'subject:handle': 0.84; 'subject:try': 0.84; 'victor': 0.84; 'to:none': 0.92; '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:cc :content-type; bh=P/d2lBrT9Ik82k8nd2Jasgw3ERwQ2Xl/fWat+AMy9rM=; b=zpAiCU+kHC6kl0vH4jvzrIQyTtCo2ltIgKWKWDbBEPL9hDW4WvXA++x2dHdA9fflrf S0pgdZmX2JEy8P+rDiZ+hkh/6QC4WJB+uzGxDIJDI5rlzX2MzTxaNqqW50bdf0wEgonU IK4la3UfPJ2jEfbWJrgmVX9yM+BvVpOCycQO4nWAsx3JZkGc2HtgHuAWJdWIr6U4AJN/ QJl1eR49eDsGBpPdQ3sjih3HZiPe7/QDdY+P7UJR/2iGsxIJOttL0c36t2wasmkKMlMk yXNlhQ9mvZGABnNE2eC/r1G3PJnyJIy0PfRo6qnOr+BRGR3ViNqL3kho7cNpwnOQ/+Y5 4jvQ== MIME-Version: 1.0 X-Received: by 10.180.211.237 with SMTP id nf13mr25726717wic.55.1384959903151; Wed, 20 Nov 2013 07:05:03 -0800 (PST) In-Reply-To: <528c16b5$0$29992$c3e8da3$5496439d@news.astraweb.com> References: <8379f7c2-c248-4a67-82ed-2d288a1635d2@googlegroups.com> <4c5f2389-177c-4056-8857-19e2950f8aa7@googlegroups.com> <528c16b5$0$29992$c3e8da3$5496439d@news.astraweb.com> Date: Wed, 20 Nov 2013 10:05:03 -0500 Subject: Re: Using try-catch to handle multiple possible file types? From: Neil Cerutti Cc: 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: 122 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384959910 news.xs4all.nl 15872 [2001:888:2000:d::a6]:44089 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60090 Steven D'Aprano steve+comp.lang.python@pearwood.info via python.org 8:56 PM (12 hours ago) wrote: > Write a helper function: > > def process(opener): > with opener('blah.txt', 'rb') as f: > for line in f: > print(line) As another option, you can enter the context manager after you decide. try: f = gzip.open('blah.txt', 'rb') except IOError: f = open('blah.txt', 'rb') with f: # processing for line in f: print(line) contextlib.ExitStack was designed to handle cases where entering context is optional, and so also works for this use case. with contextlib.ExitStack() as stack: try: f = gzip.open('blah.txt', 'rb') except IOError: f = open('blah.txt', 'rb') stack.enter_context(f) for line in f: print(line) -- Neil Cerutti On Tue, Nov 19, 2013 at 8:56 PM, Steven D'Aprano wrote: > On Tue, 19 Nov 2013 16:30:46 -0800, Victor Hooi wrote: > >> Hi, >> >> Is either approach (try-excepts, or using libmagic) considered more >> idiomatic? What would you guys prefer yourselves? > > Specifically in the case of file types, I consider it better to use > libmagic. But as a general technique, using try...except is a reasonable > approach in many situations. > > >> Also, is it possible to use either approach with a context manager >> ("with"), without duplicating lots of code? >> >> For example: >> >> try: >> with gzip.open('blah.txt', 'rb') as f: >> for line in f: >> print(line) >> except IOError as e: >> with open('blah.txt', 'rb') as f: >> for line in f: >> print(line) >> >> I'm not sure of how to do this without needing to duplicating the >> processing lines (everything inside the with)? > > Write a helper function: > > def process(opener): > with opener('blah.txt', 'rb') as f: > for line in f: > print(line) > > > try: > process(gzip.open) > except IOError: > process(open) > > > If you have many different things to try: > > > for opener in [gzip.open, open, ...]: > try: > process(opener) > except IOError: > continue > else: > break > > > > [...] >> Also, on another note, python-magic will return a string as a result, >> e.g.: >> >> gzip compressed data, was "blah.txt", from Unix, last modified: Wed Nov >> 20 10:48:35 2013 >> >> I suppose it's enough to just do a? >> >> if "gzip compressed data" in results: >> >> or is there a better way? > > *shrug* > > Read the docs of python-magic. Do they offer a programmable API? If not, > that kinda sucks. > > > > -- > Steven > -- > https://mail.python.org/mailman/listinfo/python-list -- Neil Cerutti