How to prepend lines to stdout ?
In other words, when I write to stdout I want the output that was already printed to be 'pushed' down, then prepend next output.
How to prepend lines to stdout ?
In other words, when I write to stdout I want the output that was already printed to be 'pushed' down, then prepend next output.
You cannot do that simply by manupulating stdout directly. Stdout is a file object. In regular file objects seek
can be used. However, since everything is displayed in terminal as quickly as they get written to it, calling seek doesn't affect what has already been displayed.
However, with a proper terminal library like curses you can output control characters that manipulate cursor position and allows you to change text content anywhere in the console.
Note that curses is pretty low level library. For your specific use case of making animations, it might make sense to use a higher-level library like asciimatics.
Store the initial value to list,
then until your condition is satisfied,
join the data with '\n'
That is,
import sys
arr = ['start line',]
while some_condition_till_you_want_to_write:
arr.append('some new content')
print "\n".join(arr)
sys.stdout.flush()