Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #59962
| References | <8379f7c2-c248-4a67-82ed-2d288a1635d2@googlegroups.com> |
|---|---|
| From | Amit Saha <amitsaha.in@gmail.com> |
| Date | 2013-11-19 17:22 +1000 |
| Subject | Re: Using try-catch to handle multiple possible file types? |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2890.1384846203.18130.python-list@python.org> (permalink) |
On Tue, Nov 19, 2013 at 5:13 PM, Victor Hooi <victorhooi@gmail.com> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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