2

My code snippet look like this:

from time import sleep
for i in xrange(10):
    status = "hello%s" % str(i)
    status = status + chr(8)*(len(status)+1)
    sleep(1)
    print status

which behaves differently with

print status

and

print status,

Now when i use a comma after print status then print outputs to stdout only once. i.e. after the loop iterates for the last time, where as when i don't put that comma after print status then print outputs to stdout each time it is called (as i expect it to).

I dont understand what is happening underneath. Can anyone explain. thanks :)

Shiv Deepak
  • 3,122
  • 5
  • 34
  • 49
  • http://docs.python.org/reference/simple_stmts.html#the-print-statement seems clear. Please reference the **specific** parts of the documentation that confuse you. Please **update** the question to explain what part of this link is confusing. – S.Lott Mar 05 '11 at 13:01

3 Answers3

5

The console terminal is line buffered which mean that the console buffer will be flushed when it will found a new line character \n , which explain the behavior that you see now, because when you do print status print will print automatically a new line character which will flush the console buffer after the print , but when you do print status, the print will not add the new line so the buffer will not be flushed after the print.

Here is a good article about buffering hope it can give you more insight :)

mouad
  • 67,571
  • 18
  • 114
  • 106
  • ohh! yes. i really missed that :) – Shiv Deepak Mar 05 '11 at 11:21
  • but i dont understand how it worked?? --> http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python/22776#22776 is it something to do with raw string? – Shiv Deepak Mar 05 '11 at 11:26
  • @Shiv Deepak : i think in that case the Backspace (chr(8)) will flush the console terminal by removing all the character `chr(8)*(len(status)+1)`, try removing this part of the status string and you will see that the console will print by blocks (taking in consideration that when the buffer is full it will be flushed) , hope this help :) – mouad Mar 05 '11 at 11:39
2

Even with comma it works fine, without newline though : http://codepad.org/iXK0o8Cu

This is per behaviour as told here : http://www.ferg.org/projects/python_gotchas.html#contents_item_4

comma removes the newline after print

DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
  • that is not the output. output would be just `hello9` but would be dynamic i.e it would iterate from `hello0` to `hello9` reusing the same buffer. – Shiv Deepak Mar 05 '11 at 11:18
1

it is something with buffering. Like in C, if you print something to standard output and then you close it, you have got to call fflush() to flush out buffer to output. If you import sys at the beginning and after print you call "sys.stdout.flush()", it works.

Garret Raziel
  • 375
  • 1
  • 8
  • 20
  • It works on my machine. It erases every previous "helloX" and prints new. Or, perhaps, it is not wanted behaviour, if so, then sorry. – Garret Raziel Mar 05 '11 at 11:22