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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:09:01+00:00 2026-06-03T22:09:01+00:00

I am trying to teach myself Python by reading documentation. I am trying to

  • 0

I am trying to teach myself Python by reading documentation. I am trying to understand what it means to flush a file buffer. According to documentation, “file.flush” does the following.

Flush the internal buffer, like stdio‘s fflush().
This may be a no-op on some file-like objects.

I don’t know what “internal buffer” and “no-op” mean, but I think it says that flush writes data from some buffer to a file.

Hence, I ran this file toggling the pound sign in the line in the middle.

with open("myFile.txt", "w+") as file:
    file.write("foo")
    file.write("bar")
    # file.flush()
    file.write("baz")
    file.write("quux")

However, I seem to get the same myFile.txt with and without the call to file.flush(). What effect does file.flush() have?

  • 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-03T22:09:03+00:00Added an answer on June 3, 2026 at 10:09 pm

    Python buffers writes to files. That is, file.write returns before the data is actually written to your hard drive. The main motivation of this is that a few large writes are much faster than many tiny writes, so by saving up the output of file.write until a bit has accumulated, Python can maintain good writing speeds.

    file.flush forces the data to be written out at that moment. This is hand when you know that it might be a while before you have more data to write out, but you want other processes to be able to view the data you’ve already written. Imagine a log file that grows slowly. You don’t want to have to wait ages before enough entries have built up to cause the data to be written out in one big chunk.

    In either case, file.close causes the remaining data to be flushed, so “quux” in your code will be written out as soon as file (which is a really bad name as it shadows the builtin file constructor) falls out of scope of the with context manager and gets closed.

    Note: your OS does some buffering of its own, but I believe every OS where Python is implemented will honor file.flush‘s request to write data out to the drive. Someone please correct me if I’m wrong.

    By the way, “no-op” means “no operation”, as in it won’t actually do anything. For example, StringIO objects manipulate strings in memory, not files on your hard drive. StringIO.flush probably just immediately returns because there’s not really anything for it to do.

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

Sidebar

Related Questions

I have been trying to teach myself Regexes in python and I decided to
I am trying to teach myself MySQL/PHP from the very beginning. The following code
I am trying to teach myself Python, and I have realized that the only
I am trying to teach myself C from a python background. My current mini-problem
I'm trying to teach myself CSS and have the following markup: <style type=text/css> #content
I'm trying to teach myself python using Google's AppEngine , and I can't get
I am trying to teach myself jquery while reading someone else's code, and I
I am trying to teach myself pthreads threading. I have the following source, which
I'm trying to teach myself Python and found a couple of exercises online. One
I'm trying to teach myself about buffer overflows and exploitation in C++. I'm an

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.