Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #60090

Re: Using try-catch to handle multiple possible file types?

References <8379f7c2-c248-4a67-82ed-2d288a1635d2@googlegroups.com> <mailman.2897.1384853817.18130.python-list@python.org> <4c5f2389-177c-4056-8857-19e2950f8aa7@googlegroups.com> <528c16b5$0$29992$c3e8da3$5496439d@news.astraweb.com>
Date 2013-11-20 10:05 -0500
Subject Re: Using try-catch to handle multiple possible file types?
From Neil Cerutti <mr.cerutti@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2967.1384959910.18130.python-list@python.org> (permalink)

Show all headers | View raw


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
<steve+comp.lang.python@pearwood.info> 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 <mr.cerutti+python@gmail.com>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Using try-catch to handle multiple possible file types? Victor Hooi <victorhooi@gmail.com> - 2013-11-18 23:13 -0800
  Re: Using try-catch to handle multiple possible file types? Chris Angelico <rosuav@gmail.com> - 2013-11-19 18:22 +1100
  Re: Using try-catch to handle multiple possible file types? Amit Saha <amitsaha.in@gmail.com> - 2013-11-19 17:22 +1000
  Re: Using try-catch to handle multiple possible file types? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-19 09:36 +0000
    Re: Using try-catch to handle multiple possible file types? Victor Hooi <victorhooi@gmail.com> - 2013-11-19 16:30 -0800
      Re: Using try-catch to handle multiple possible file types? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-20 01:56 +0000
        Re: Using try-catch to handle multiple possible file types? Neil Cerutti <mr.cerutti@gmail.com> - 2013-11-20 10:05 -0500
      Re: Using try-catch to handle multiple possible file types? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-20 01:50 +0000

csiph-web