I want to run a script, which basicly shows things like:
Installing XXX... [DONE]
Now, at the moment, I use print to print the whole line AFTER the function has succeeded. However, I now want it to print "Installing xxx..." first, and AFTER the function has run, to add the "DONE" tag; but on the same line.
Any ideas?
sys.stdout.write
will print without return carriagehttp://en.wikibooks.org/wiki/Python_Programming/Input_and_output#printing_without_commas_or_newlines
Most simple:
print(‘\r’+’something to be override’,end=‘’)
It means it will back the cursor to beginning, than will print something and will end in the same line. If in a loop it will start printing in the same place it starts.
Here a 2.7-compatible version derived from the 3.0 version by @Vadim-Zin4uk:
Python 2
For that matter, the 3.0 solution provided looks a little bloated. For example, the backspace method doesn't make use of the integer argument and could probably be done away with altogether.
Python 3
Both have been tested and work.
Use
sys.stdout.write('Installing XXX... ')
andsys.stdout.write('Done')
. In this way, you have to add the new line by hand with"\n"
if you want to recreate the print functionality. I think that it might be unnecessary to use curses just for this.I found this solution, and it's working on Python 2.7
You can simply use this:
and the output will be
no need to overkill by
import sys
. Pay attention to comma symbol at the end.Python 3+
print("some string", end="");
to remove the newline insert at the end. Read more byhelp(print);