Search code examples
pythonfor-looptimeprintingsleep

Why when I convert a .py file to an .exe terminal based program, a print in a for loop isn't printing until the loop has ended?


I want to print a dot every second 60 times, when I run the application from the IDE, it works fine but when I convert the file to an exe, it doesn't print the dots until the for loop has ended.

This is the code:

import time

for i in range(60):
    print(".", end="")
    time.sleep(1) 

Sorry if it is a dumb question, I am new to python


Solution

  • Try adding flush = True to your print function.

    The problem is likely buffering in the output of your program, which gets flushed traditionally when the printed content reaches a sufficient size, on newlines, and on exiting the program.

    Manually adding the flush parameter will flush the output every time you call the print function. See the print function documentation.