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

  • Home
  • SEARCH
  • 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 3501248
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:50:07+00:00 2026-05-18T12:50:07+00:00

Had an interesting experience with Python’s file buffering and wanted to know that I

  • 0

Had an interesting experience with Python’s file buffering and wanted to know that I understand it correctly. Given

[Python 2.7 shell]

...
model = (really big Numpy model)
f = open('file.out','w')
pickle.dump(model, f)
(pickle.dump() finishes while I'm doing other things)

[Bash shell]

$ ls -l
-rw-r--r--  1 john  staff  270655488 Dec  6 21:32 file.out

[Return to Python shell]

model = (different really big Numpy model)
f = open('newfile.out','w')
pickle.dump(model,f)
(pickle.dump() finishes)

[Bash shell]

$ ls -l
-rw-r--r--  1 john  staff  270659455 Dec  7 07:09 file.out
-rw-r--r--  1 john  staff  270659451 Dec  6 20:48 newfile.out

Note file.out is now a different size.

Now, I know that Python’s file buffer defaults to the system size (I’m on Mac OSX), so it seems that there were still 3,967 bytes in the file buffer while I was screwing around, and the Mac OSX file buffer is greater than that.

What interested me was that I was forcibly reassigning the file object ‘f’ to another open file without actually calling f.close() (Honestly, I was just working really fast to test something else and forgot). When I looked at the file size, I half expected it to remain the same (which might mean truncating the output)

So, the question is whether this is a safe procedure. Is the file object assignment wrapped in such a way that either the Python garbage collector, or the file object itself, flushes the buffer and closes the file on such a sudden variable re-assignment even if you don’t call the close() method? More importantly, is this always the case, or is it possible that the variable re-assignment actually did— or in another situation might— truncate that buffer before the file buffer flushed.

I guess it’s really a question of how elegant and safe the file objects and Python garbage collector are when yanking objects around without appropriate destruction.

  • 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-18T12:50:08+00:00Added an answer on May 18, 2026 at 12:50 pm

    As long as your computer does not crash, you won’t lose data by not closing a file. Python does indeed close files if the corresponding file objects are garbage collected. In the case you described, the name f was the only reference to the file, so it was closed when you used the name for something else.

    Note that it is good practice to close files anyway to free the system ressources allocated by the file object. In some situations you don’t know exactly when a file object will be garbage collected — for example in case of an error, a reference to the file object might be stored in the traceback object, preventing garbage collection. All files are closed when the interpreter exits.

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

Sidebar

Related Questions

I have run into an interesting bug where it appears that when you select
I was wondering if any developers in the embedded space know of any interesting
I know that there are a variety of similarly posed questions here on SO
I had an interesting request from a client today and I'm not exactly sure
I've recently found an interesting alternative to the ASP.NET MVC that I currently do:
Background : It had been a while since I had read https://stackoverflow.com/questions/228181/the-zen-of-python so I
Recently I had cause to compare Blowfish algorithms. I was comparing outputs from DI
I've had to do this many times in the past, and I've never been
I'm a Django newbie and am interesting in understanding how to install the EmailAuth
anyone know the meaning of %s in joomla ? I found it in .ini

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.