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 6674341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:44:01+00:00 2026-05-26T03:44:01+00:00

I need to have a file deleted if not all the operations that must

  • 0

I need to have a file deleted if not all the operations that must be done on it were successful (that is, if an exception is raised). It could have been as simple as using except:, deleting the file and then re-raising the exception, but in that case the original exception would be lost if the file cannot be deleted in the except clause for whatever arcane reason.

The best that I have been able to come up with is this:

try:
    file_path = "whatever.jpg"
    # do stuff with file
except:
    exception_raised = True
    raise
finally:
    try:
        if exception_raised:
            os.unlink(file_path)
    except:
        pass

return file_path   # everything OK

Does anybody know of a better, more Pythonic approach?

  • 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-05-26T03:44:02+00:00Added an answer on May 26, 2026 at 3:44 am

    Another option is to simply store the exception if you don’t want to lose it:

    Python 3.x version:

    try:
        file_path = "whatever.jpg"
        # do stuff with file
    except BaseException as e:
        try:
            os.unlink(file_path)
        except Exception:
            traceback.print_exc()
        raise e
    

    The Python 2.x version is slightly more complex since you need to store the complete exception information manually (otherwise you’d lose the traceback):

    try:
        file_path = "whatever.jpg"
        # do stuff with file
    except:
        e = sys.exc_info()
        try:
            os.unlink(file_path)
        except Exception:
            traceback.print_exc()
        raise e[0], e[1], e[2]
    

    Edit: Only catch subclasses of Exception in the inner try block, since you don’t want do catch SystemExit or KeyboardInterrupt here. Also report any excpetion that occurred during unlinking instead of just dropping it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a huge file that has some lines that need to have a
I have a file that I need to parse into an array but I
I need to insert, delete and update data using a xml file. I have
I am newbie in Python and I need help: I have a file with
In the web page, I have file upload widget. I need to show this
I have js file where i need to call the function defined in the
I have this file which I need to read the first bytes to check
I have a file and searching mutiple value i.e I need to search {Name,Class}
I have a file as documentRoot/app/webroot/myFile.php I need to be able to access this
I have text file with some text information and i need to split this

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.