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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:06:15+00:00 2026-06-13T09:06:15+00:00

I have classes that can take a file as an argument, for example: ParserClass(file(‘/some/file’,

  • 0

I have classes that can take a file as an argument, for example:

ParserClass(file('/some/file', 'rb'))

If I understand Python correctly, the file will be closed automatically once the object is garbage collected. What I don’t understand is exactly when that happens. In a function like:

def parse_stuff(filename):
    parser = ParserClasss(file(filename, 'rb'))

    return list(parser.info())

Shouldn’t that parser object be GC’d immediately after the function exits, causing the file to be closed? Yet, for some reason, Python appears to have the file open long after the function exits. Or at least it looks that way because Windows won’t let me modify the file, claiming Python has it open and forcing me to to close IDLE.

Is there a way to ensure files are closed, short of explicitly asking for it for every file I create? I also want to add that these classes are external, I don’t want to dig through them to find out exactly what they with the file.

  • 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-13T09:06:16+00:00Added an answer on June 13, 2026 at 9:06 am

    You can use with to open files. When you use with, the file will be implicitly closed when the with block is exited, and it will handle exception states as well.

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

Sidebar

Related Questions

Why can NHibernate create a proxy for classes with properties that have private setters
I have some classes that are used for Styling and all of them display
I have some classes in C++ that I would like to expose to Lua.
got a question regarding serializing classes that I've defined. I have some classes like
I have classes that are named exactly the same across different plug-ins that I
I have classes that are needed in both my web service and my server.
I have three classes that I am using and have shortened for ease of
I have 2 classes that have exact two functions. The difference between them is
I have custom classes that I currently instantiate within App.xaml as resources. I want
I have two classes that refer to each other, but obviously the compiler complains.

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.