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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:47:54+00:00 2026-06-09T05:47:54+00:00

The Python File Type documentation gives file.closed as a bool indicating the current state

  • 0

The Python File Type documentation gives file.closed as a

bool indicating the current state of the file object. This is a read-only attribute; the close() method changes the value. It may not be available on all file-like objects.

Given that it’s not guaranteed to be available on all file-like objects, is there another (better?) method of checking whether I’ve already got the file open, before trying to re-open it?

  • 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-09T05:47:55+00:00Added an answer on June 9, 2026 at 5:47 am

    All file objects returned by open have the closed attribute.

    File-like objects are objects that implement the same interface as file (for example StringIO.StringIO), and the doc you’ve quoted indicates that it is not necessary to have a closed attribute in order to implement such an interface.

    The same docs have notes sprinkled all over about what methods should be implemented in which file-like objects. They also have this to say about attributes:

    File objects also offer a number of other interesting attributes. These are not required for file-like objects, but should be implemented if they make sense for the particular object.

    Since the docs for the close methods state that “any operation which requires that the file be open will raise a ValueError“, the best way to check if a file-like object is closed is to simply use the file as if it were open, and catch ValueError in case it is not.

    try:
        mystr = f.read(42)
    except ValueError:
        # f was closed, do whatever needs doing
    

    Not all file-like objects are seek-able, so doing a test read to check if the object is closed (as I suggested to begin with) is a bad idea. This is because then you need to store what you’ve read in case the file turns out to be open, and then somehow prepend it to the actual read, when it happens. It’s better to simply assume it is open and do whatever you want to do right away, and then deal with closed files in the except clause.

    As has been pointed out in the comments, one does not simply re-open a closed file-like object. In the StringIO example, closing actually deletes the buffer, so the file-like object no longer has the information required to “re-open” itself as it used to be before closing.

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

Sidebar

Related Questions

I have this test python file import os print 'Content-type: text/html' print print '<HTML><HEAD><TITLE>Python
I have this python file that I'm working on: class Range: An object that
I have a Python file with as content: import re import urllib class A(object):
There is a python file named BasePlat.py which contain something like this: class BasePlatform(SimObj):
I am using Komodo edit on a Python file on Windows. When I type
The Python version of Google protobuf gives us only: SerializeAsString() Where as the C++
Is there a Python file type for accessing random lines without traversing the whole
In another question here I've read that python-magic should output the correct mime type
How do I find the program associated with a given file type using Python
When launching a script-type python file from Windows you get a windows shell type

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.