Output to the same line overwriting previous output?
Here's code for Python 3.x:
print(os.path.getsize(file_name)/1024+'KB / '+size+' KB downloaded!', end='\r')
The end=
keyword is what does the work here -- by default, print()
ends in a newline (\n
) character, but this can be replaced with a different string. In this case, ending the line with a carriage return instead returns the cursor to the start of the current line. Thus, there's no need to import the sys
module for this sort of simple usage. print()
actually has a number of keyword arguments which can be used to greatly simplify code.
To use the same code on Python 2.6+, put the following line at the top of the file:
from __future__ import print_function
How to overwrite the previous print to stdout?
Simple Version
One way is to use the carriage return ('\r'
) character to return to the start of the line without advancing to the next line.
Python 3
for x in range(10):
print(x, end='\r')
print()
Python 2.7 forward compatible
from __future__ import print_function
for x in range(10):
print(x, end='\r')
print()
Python 2.7
for x in range(10):
print '{}\r'.format(x),
print
Python 2.0-2.6
for x in range(10):
print '{0}\r'.format(x),
print
In the latter two (Python 2-only) cases, the comma at the end of the print statement tells it not to go to the next line. The last print statement advances to the next line so your prompt won't overwrite your final output.
Line Cleaning
If you can’t guarantee that the new line of text is not shorter than the existing line, then you just need to add a “clear to end of line” escape sequence, '\x1b[1K'
('\x1b'
= ESC):
for x in range(75):
print('*' * (75 - x), x, end='\x1b[1K\r')
print()
Overwriting a line after printing out something
By default, print
adds a newline, '\n'
, after all arguments have been printed, moving you to the next line. Pass it end='\r'
(carriage return) to make it return to the beginning of the current line without advancing to the next line. To be sure output isn't buffered (stdout
is typically line buffered when connected to a terminal), make sure to pass flush=True
to print
as well, for a final result of:
for n in range(101):
timefluc = random.uniform(0, 1.2)
time.sleep(timefluc)
print("{0}%".format(n), end='\r', flush=True)
Python 3.8: Overwrite and clear previous shorter line in terminal
I have found a decent work around for this problem. You can just fill the end of the string with white spaces with f strings. The full code for the problem I stated in the question would then be:
print('Tessst', end='\r')
print(f'{"Test" : <10}')
I found this way here
Related Topics
Matplotlib Scatterplot; Color as a Function of a Third Variable
Get a Variable from the Url in a Flask Route
How to Call a Python Function from Node.Js
Python's Time.Clock() VS. Time.Time() Accuracy
How to Remove the Ansi Escape Sequences from a String in Python
Pip How to Remove Incorrectly Installed Package with a Leading Dash: "-Pkgname"
Access Multiple Elements of List Knowing Their Index
Urllib2.Httperror: Http Error 403: Forbidden
What Exactly Does the .Join() Method Do
Class Method Differences in Python: Bound, Unbound and Static
Iterate Over All Pairs of Consecutive Items in a List
Interleave Multiple Lists of the Same Length in Python
Storing Value from a Parsed Ping
Differencebetween Dict.Items() and Dict.Iteritems() in Python2