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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:15:42+00:00 2026-05-28T11:15:42+00:00

Sometimes when using ipython you might hit an exception in a function which has

  • 0

Sometimes when using ipython you might hit an exception in a function which has opened a file in write mode. This means that the next time you run the function you get a value error,

ValueError: The file ‘filename’ is already opened. Please close it before reopening in write mode.

However since the function bugged out, the file handle (which was created inside the function) is lost, so it can’t be closed. The only way round it seems to be to close the ipython session, at which point you get the message:

Closing remaining open files: filename… done

Is there a way to instruct ipython to close the files without quitting the session?

  • 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-28T11:15:42+00:00Added an answer on May 28, 2026 at 11:15 am

    You should try to always use the with statement when working with files. For example, use something like

    with open("x.txt") as fh:
        ...do something with the file handle fh
    

    This ensures that if something goes wrong during the execution of the with block, and an exception is raised, the file is guaranteed to be closed. See the with documentation for more information on this.

    Edit: Following a discussion in the comments, it seems that the OP needs to have a number of files open at the same time and needs to use data from multiple files at once. Clearly having lots of nested with statements, one for each file opened, is not an option and goes against the ideal that “flat is better than nested”.

    One option would be to wrap the calculation in a try/finally block. For example

    file_handles = []
    try:
        for file in file_list:
            file_handles.append(open(file))
    
        # Do some calculations with open files
    
    finally:
        for fh in file_handles:
            fh.close()
    

    The finally block contains code which should be run after any try, except or else block, even if an exception occured. From the documentation:

    If finally is present, it specifies a “cleanup” handler. The try clause is executed, including any except and else clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The finally clause is executed. If there is a saved exception, it is re-raised at the end of the finally clause. If the finally clause raises another exception or executes a return or break statement, the saved exception is lost. The exception information is not available to the program during execution of the finally clause.

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

Sidebar

Related Questions

I'm sometimes confused to using which one of them, say I have a function
While using Vim I'll sometimes want to look at a function definition or a
I've come across this twice now. Sometimes using the following line of code: [self.navigationController
I am using several API's and MySQL databases which sometimes throw error because of
I am using Ogre3D and sometimes when there is a break (or an exception)
I have a window that I sometimes open using Show() and sometimes using ShowDialog()
I develop code using .net, and sometimes using ruby. My friend wants me to
Sometimes when using eclipse it loses references to the JRE. i.e. It cannot find
My eclipse sometimes starts using 100 % of my CPU very spontaneously. I can't
When using Mercurial I sometimes find that it is hard to understand the relationship

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.