[Python] Why doesn't print output show up immediately in the terminal when there is no newline...

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Python script that performs a simulation. It takes a fairly long, varying time to run through each iteration, so I want to print a . after each loop as a way to monitor how fast it runs and how far it went through the for statement as the script runs. The code has this general structure:

    for step in steps:
    run_simulation(step)
    # Python 3.x version:
    print('.', end='')
    # for Python 2.x:
    # print '.',


    However, when I run the code, the dots do not appear one by one. Instead, all the dots are printed at once when the loop finishes, which makes the whole effort pointless. How can I print the dots inline as the code runs?


    This problem can also occur when iterating over data fed from another process and trying to print results, for example to echo input from an Electron app. See Python not printing output.

    Continue reading...

Compartilhe esta Página