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


Groups > comp.lang.python > #60047

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

From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Using try-catch to handle multiple possible file types?
Date 2013-11-20 01:50 +0000
References <8379f7c2-c248-4a67-82ed-2d288a1635d2@googlegroups.com> <mailman.2897.1384853817.18130.python-list@python.org> <4c5f2389-177c-4056-8857-19e2950f8aa7@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.2948.1384913119.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 20/11/2013 00:30, Victor Hooi wrote:
> Hi,
>
> Is either approach (try-excepts, or using libmagic) considered more idiomatic? What would you guys prefer yourselves?
>
> 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)?
>
> And using:
>
> try:
> 	f = gzip.open('blah.txt', 'rb')
> except IOError as e:
> 	f = open('blah.txt', 'rb')
> finally:
> 	for line in f:
> 		print(line)
>
> won't work, since the exception won't get thrown until you actually try to open the file. Plus, I'm under the impression that I should be using context-managers where I can.
>
> 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?
>
> Cheers,
> Victor
>
> On Tuesday, 19 November 2013 20:36:47 UTC+11, Mark Lawrence  wrote:
>> On 19/11/2013 07:13, Victor Hooi wrote:
>>
>>>
>>
>>> So basically, using exception handling for flow-control.
>>
>>>
>>
>>> However, is that considered bad practice, or un-Pythonic?
>>
>>>
>>
>>
>>
>> If it works for you use it, practicality beats purity :)
>>
>>
>>
>> --
>>
>> Python is the second best programming language in the world.
>>
>> But the best has yet to be invented.  Christian Tismer
>>
>>
>>
>> Mark Lawrence

Something like

for filetype in filetypes:
   try:
     process(filetype)
     break
   except IOError:
     pass

??? as it's 01:50 GMT and I can't sleep :(

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

Back to comp.lang.python | Previous | NextPrevious 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