There seems to be a difference in how
for i in xrange(10):
print "Working" ,
-u Force stdin, stdout and stderr to be totally unbuffered.
Assuming you're talking about CPython (likely), this has to do with the behaviour of the underlying C implementations.
The ISO C standard mentions (
C11 7.21.3 Files /3) three modes:
There are other triggers that cause the characters to appear (such as buffer filling up even if no newline is output, requesting input under some circumstances, or closing the stream) but they're not important in the context of your question.
What is important is
7.21.3 Files /7 in that same standard:
As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.
Note the wiggle room there. Standard output can either be line buffered or unbuffered unless the implementation knows for sure it's not an interactive device.
In this case (the console), it is an interactive device so the implementation is not permitted to use unbuffered. It is, however allowed to select either of the other two modes which is why you're seeing the difference.
Unbuffered output would see the messages appear as soon as you output them (a la your Windows behaviour). Line-buffered would delay until output of a newline character (your Linux behaviour).
If you really want to ensure your messages are flushed regardless of mode, just flush them yourself:
import time, sys for i in xrange(10): time.sleep(1) print "Working", sys.stdout.flush() print
In terms of guaranteeing that output will be buffered when redirecting to a file, that would be covered in the quotes from the standard I've already shown. If the stream can be determined to be using a non-interactive device, it will be fully buffered. That's not an absolute guarantee since it doesn't state how that's determined but I'd be surprised if any implementation couldn't figure that out.
In any case, you can test specific implementations just by redirecting the output and monitoring the file to see if it flushes once per output or at the end.