Given a file of unknown file type, I'd like to open that file with one of a number of handlers. Each of the handlers raises an exception if it cannot open the file. I would like to try them all and if none succeeds, raise an exception.
The design I came up with is
filename = 'something.something' try: content = open_data_file(filename) handle_data_content(content) except IOError: try: content = open_sound_file(filename) handle_sound_content(content) except IOError: try: content = open_image_file(filename) handle_image_content(content) except IOError: ... This cascade doesn't seem to be the right way to do it.
Any suggestions?
withwill remove one or two levels oftry/exceptfor opener, handler in [(open_data_file, handle_data_content), ...]:?