What Is the Purpose of flush=True in the print() Function?
What Is the Purpose of flush=True in the print() Function?
349
21-Mar-2025
Khushi Singh
31-Mar-2025Python's print() function with the
flush=Trueparameter enables instant streaming of output data directly to the terminal and output stream pipeline. Python follows default buffering behavior, which maintains printed data inside a temporary storage before showing it on the screen, especially while writing to files or pipes. When using theflush=Trueargument, the print function performs an immediate output, which serves well for applications needing real-time display.The terminal execution of Python implements line buffering as a normal behavior, which triggers output display only at newline (\n) occurrences. The writing process to files or the logging system results in output buffering until completion, which leads to delayed screen display.
A progress bar displayed during a real-time update cycle will display all output at once if buffering occurs because it stores data before writing to the display. The use of the flush=True parameter enables the immediate execution of each print command.
The prompt flush=True enables instant display of progress updates without waiting for the buffer to reach its maximum capacity. It provides direct output in applications that need instant feedback, such as debugging and real-time systems.