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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:27:06+00:00 2026-06-14T12:27:06+00:00

when I use this code in IPython3 shell >>>data = open(‘file’).read() and then check

  • 0

when I use this code in IPython3 shell

 >>>data = open('file').read()

and then check open file descriptors:

 lsof | grep file

I find empty list

and when I use this:

>>>open('file')

lsof shows two items. The question is why first operation closes fd while second doesn’t? I supposed that garbage collector must delete file object with no refs.

I know about ‘_’ var in interpreter, when I reassign value

>>>111
>>>_
111

but descriptors remain open.
when I repeat

>>>open('file')

n times there are 2 * n opened descriptors

  • 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-14T12:27:07+00:00Added an answer on June 14, 2026 at 12:27 pm

    In the second example the file handle is retained by the interactive interpreter variable _, which allows you to access the last evaluated expression. If you evaluate another expression, such as 1+1, you will note that the file is no longer reported by lsof as open.

    As pointed out by Mike Byers, this behavior is specific to CPython, and even then to precise circumstances of how the file object is used. To make sure the file is closed regardless of how the code is executed, use a with statement:

    with open('file') as fp:
        data = fp.read()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use this code to check if a user has an open Facebook session
I use this code to update data in database table. Can reuse same code
I use this code to load an Excel file into a datatable: public static
I use this code to create and plot N points: N=input('No. of Nodes:'); data
I use this code till I see data in my database and edit,delet and
I use this code in a Servlet which sets the file name of the
I use this code for upload image: http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/ I replaced bm = BitmapFactory.decodeFile(/data/data/fshizzle.com/files/image.jpg); and
I use this code and SQLite says Data Source cannot be empty. Use :memory:
I use this code snippet to obtain a file as an input stream. The
I use this code to check domain availability: try: w = pywhois.whois('honarasai.com') if not

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.