Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8574923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:35:05+00:00 2026-06-11T19:35:05+00:00

I have several possible files which could hold my data; they can be compressed

  • 0

I have several possible files which could hold my data; they can be compressed in different ways, so to open them I need to use file(), gzip.GzipFile() and other which also return a file object (supporting the with interface).

I want to try each of them until one succeeds in opening, so I could do something like

try:
  with gzip.GzipFile(fn + '.gz') as f:
    result = process(f)
except (IOError, MaybeSomeGzipExceptions):
  try:
    with xCompressLib.xCompressFile(fn + '.x') as f:
      result = process(f)
  except (IOError, MaybeSomeXCompressExceptions):
    try:
      with file(fn) as f:
        result = process(f)
    except IOError:
      result = "some default value"

which obviously isn’t feasible in case I have dozens of possible compression variants. (The nesting will get deeper and deeper, the code always looking very much alike.)

Is there a nicer way to spell this out?

EDIT: If possible I’d like to have the process(f) out of the try/except as well to avoid accidental catching of exceptions raised in the process(f).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:35:07+00:00Added an answer on June 11, 2026 at 7:35 pm

    I’d write a custom context manager:

    from contextlib import contextmanager
    
    filetypes = [('.gz', gzip.GzipFile, (IOError, MaybeSomeGzipExceptions)), 
                 ('.x', xCompressLib.xCompressFile, (IOError, MaybeSomeXCompressExceptions))]
    
    @contextmanager
    def open_compressed(fn):
        f = None
        try:
            for ext, cls, exs in filetypes:
                try:
                    f = cls(fn + ext)
                except exs:
                    pass
                else:
                    break
            yield f
        finally:
            if f is not None:
                f.close()
    
    with open_compressed(fn) as f:
        result = "some default value" if f is None else process(f)
    

    Or possibly just a function that returns a context manager:

    filetypes = [('.gz', gzip.GzipFile, (IOError, MaybeSomeGzipExceptions)), 
                 ('.x', xCompressLib.xCompressFile, (IOError, MaybeSomeXCompressExceptions))]
    
    class UnknownCompressionFormat(Exception):
        pass
    
    def open_compressed(fn):
        for ext, cls, exs in filetypes:
            try:
                return cls(fn + ext)
            except exs:
                pass
        raise UnknownCompressionFormat
    
    try:
        with open_compressed(fn) as f:
            result = process(f)
    except UnknownCompressionFormat:
        result = "some default value"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which has several resource files for different languages depending on
Possible Duplicate: jquery data selector I have several elements with class 'connection_name'. Each one
I have several xml files with different node structure. I want to extract xml
I have a CSS file which contains several hundred 16x16 icons. They are referenced
I need to monitor console output in Java, I have tried several different ways
Is it possible to have several consumers listening on a single MSMQ instance and
I am wondering if it is possible in ExtJS to have several values of
I have read in several places that it's possible to share the objects directory
I have several different numbers in a group that range in sizes and would
I have several xml files that are formated this way: <ROOT> <OBJECT> <identity> <id>123</id>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.