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
Answer from Kudzu on Stack OverflowHere'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
If all you want to do is change a single line, use \r. \r means carriage return. It's effect is solely to put the caret back at the start of the current line. It does not erase anything. Similarly, \b can be used to go one character backward. (some terminals may not support all those features)
import sys
def process(data):
size_str = os.path.getsize(file_name)/1024, 'KB / ', size, 'KB downloaded!'
sys.stdout.write('%s\r' % size_str)
sys.stdout.flush()
file.write(data)
I have to print a progress status, so update a print with a new one. I read many post that says you have to use "\r", so I tried this:
print(string, end="\r", flush=True)
The problem is that when the previous printed string is bigger than the second one, some stuff of old print will reamins in the new print.
Here's the full code:
import time
long_string = "AAAAA" short_string = "BB" c = 0 while True: if c % 2 == 0: print(long_string, end="\r", flush=True) else: print(short_string, end="\r", flush=True) c += 1 time.sleep(1)
I would expect that in the if it would print "AAAAA" and in the else it would print just "BB", but instead, in the else, it prints "BBAAA" because the "A" remains from previous print. Here's what I mean https://i.stack.imgur.com/rE9ep.png. I would like it would be just "BB" instead of "BBAAA".
I saw this post answer https://stackoverflow.com/questions/45263205/python-how-to-print-on-same-line-clearing-previous-text so I tried to use
os.sys.stdout.write('\033[K' + long_string + '\r')instead of print, but I still have the same problem.
How can I completely delete previous clean and make a new one without traces of the old one?
python - How to overwrite the previous print to stdout? - Stack Overflow
How to print progress on the same line in Python? - Ask a Question - TestMu AI Community
Python: print and overwrite the same line reading from a file - Stack Overflow
How Do I Print Something On The Same Line?
You really should ask questions like this in r/learnpython
In Python 3, you can supply an 'end' argument to the print function to replace the default ending character, a newline. This can be an empty string.
In Python 2, you can use system.stdout.write to have precise control of output. You can do this in Python 3 as well, of course.
More on reddit.comVideos
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()
Long Line Wrap
All these methods assume you’re not writing more than the length of the line. The carriage return only returns to the start of the current line, so if your output is longer than a line, you’ll only erase the last line.
If this is enough of a problem that you need to control it, you can disable line wrapping to keep the cursor from wrapping to the next line. (Instead, the cursor sticks to the end of the line, and successive characters overwrite.)
Line wrap is disabled with print('\x1b[7l', end='') and re-enabled with print('\x1b[7h', end=''). Note that there is no automatic re-enable of line wrap at any point: don’t leave the terminal broken if an exception ends your program!
Since I ended up here via Google but am using Python 3, here's how this would work in Python 3:
for x in range(10):
print("Progress {:2.1%}".format(x / 10), end="\r")
Related answer here: How can I suppress the newline after a print statement?
Hello r/Python! I was curious if there was a way I could print something on the same line. I want a loading bar effect like you would see in homebrew, pip, or easy_install. For example it would print "Loading ---" but after a second or two it would change to "Loading ----". Is Python capable of doing this?
Ver: Python2.7 - OS: Mac
You really should ask questions like this in r/learnpython
In Python 3, you can supply an 'end' argument to the print function to replace the default ending character, a newline. This can be an empty string.
In Python 2, you can use system.stdout.write to have precise control of output. You can do this in Python 3 as well, of course.
https://docs.python.org/2.7/library/functions.html#print
print("foobar", end="")