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

2013-11-20 Thread Neil Cerutti
Steven D'Aprano steve+comp.lang.pyt...@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 af

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

2013-11-19 Thread Mark Lawrence
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:

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

2013-11-19 Thread Steven D'Aprano
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, usin

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

2013-11-19 Thread Victor Hooi
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') a

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

2013-11-19 Thread Mark Lawrence
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 ha

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

2013-11-18 Thread Amit Saha
On Tue, Nov 19, 2013 at 5:13 PM, Victor Hooi 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

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

2013-11-18 Thread Chris Angelico
On Tue, Nov 19, 2013 at 6:13 PM, Victor Hooi 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-

Using try-catch to handle multiple possible file types?

2013-11-18 Thread Victor Hooi
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