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

The Archive Base Latest Questions

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

I have a class that opens a file for writing. In my destructor, I

  • 0

I have a class that opens a file for writing. In my destructor, I call the function that closes the file:

class MyClass:
    def __del__(self):
        self.close()

    def close(self):
        if self.__fileHandle__ is not None:
                self.__fileHandle__.close()

but when I delete the object with code like:

myobj = MyClass()
myobj.open()
del myobj

if I try to reinstantiate the object, I get a value error:

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

whereas if I call myobj.close() before del myobj I don’t get this problem. So why isn’t __del__() getting called?

  • 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-28T05:21:43+00:00Added an answer on May 28, 2026 at 5:21 am

    Are you sure you want to use __del__? There are issues with __del__ and garbage collection.

    You could make MyClass a context manager instead:

    class MyClass(object):
        def __enter__(self):
            return self
        def __exit__(self,ext_type,exc_value,traceback):
            if self.__fileHandle__ is not None:
                    self.__fileHandle__.close()
    

    By doing so, you could use MyClass like this:

    with MyClass() as myobj:
        ...
    

    and myobj.__exit__ (and thus self.__fileHandle__.close()) will be called when Python leaves the with-block.

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

Sidebar

Related Questions

I have a Java program that opens a file using the RandomAccessFile class. I'd
I have a very simple class that opens a file and creates a memory
I have an application that opens another class using intent : private void createRepository(){
I'm writing a plug-in for an application where I have a custom class that
So I am writing a C# console application. I have a text file that
I have a File class that has an Open() method. I have a subclass
I have the following function for writing to a file: public void writeToFile(String data)
I have an Python ExcelDocument class that provides basic convenience methods for reading/writing/formatting Excel
I have to open a page from class extended from javax.swing.AbstractAction class... Is that
I have class that represents users. Users are divided into two groups with different

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.