Assuming you're talking about CPython (probably), this is due to the behavior of the underlying C implementations.
The ISO C standard mentions ( C11 7.21.3 Files /3 ) three modes:
- unbuffered (characters appear as soon as possible);
- fully buffered (characters appear when the buffer is full); and
- line is buffered (characters appear on the output of a new line).
There are other triggers that cause characters to appear (for example, filling a buffer even if a new line is not output, a request for input in some circumstances, or closing a stream), but they are not important in the context of your question.
The important thing is that 7.21.3 Files /7 in the same standard:
As originally discovered, the standard error stream is not fully buffered; standard input and standard output streams are fully buffered if and only if a stream can be defined so as not to refer to an interactive device.
Pay attention to the place to maneuver. Standard output can be either buffered or unbuffered, unless the implementation knows that it is not an interactive device.
In this case (the console) this is an interactive device, so the implementation is not allowed to use unbuffered. However, you are allowed to choose either of the other two modes, so you see the difference.
Unbuffered output will see that messages appear as soon as you print them (a la your Windows). The line buffer will linger until a newline is output (your behavior on Linux).
If you really want your messages to be discarded regardless of mode, simply flush them yourself:
import time, sys for i in xrange(10): time.sleep(1) print "Working", sys.stdout.flush() print
In terms of ensuring that the output will be buffered when redirecting to a file, this will be examined in quotation marks from the standard that I have already shown. If a stream can be defined as using a non-interactive device, it will be fully buffered. This is not an absolute guarantee, since it does not indicate how it is defined, but I would be surprised if any implementation could not understand this.
In any case, you can test specific implementations by simply redirecting the output and checking the file to make sure that it is reset once per output or at the end.