Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #59961
| References | <8379f7c2-c248-4a67-82ed-2d288a1635d2@googlegroups.com> |
|---|---|
| Date | 2013-11-19 18:22 +1100 |
| Subject | Re: Using try-catch to handle multiple possible file types? |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2889.1384845774.18130.python-list@python.org> (permalink) |
On Tue, Nov 19, 2013 at 6:13 PM, Victor Hooi <victorhooi@gmail.com> 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
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