Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python output above the last printed line

Is there a way in python to print something in the command line above the last line printed? Or, similarly to what I want to achieve, remain the last line intact, that is, not overwrite it.

The goal of this is to let the last line in the command line a status/precentage bar.

Output example:

File 1 processed
(0.1% Completed)

Next refresh:

File 1 processed
File 2 processed
(0.2% Completed)

Next refresh:

File 1 processed
File 2 processed
File 3 processed
(0.3% Completed)
like image 369
PyCV Avatar asked Apr 12 '17 15:04

PyCV


People also ask

How do you overwrite the last line in Python?

Approach. By default, Python's print statement ends each string that is passed into the function with a newline character, \n . This behavior can be overridden with the function's end parameter, which is the core of this method. Rather than ending the output with a newline, we use a carriage return.

What does '\ r do in Python?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

How do you overwrite a printed line in Python?

Summary: The most straightforward way to overwrite the previous print to stdout is to set the carriage return ( '\r' ) character within the print statement as print(string, end = "\r") . This returns the next stdout line to the beginning of the line without proceeding to the next line.

What does end =' in Python?

The end parameter in the print function is used to add any string. At the end of the output of the print statement in python. By default, the print function ends with a newline. Passing the whitespace to the end parameter (end=' ') indicates that the end character has to be identified by whitespace and not a newline.


2 Answers

from time import sleep
erase = '\x1b[1A\x1b[2K'

def download(number):
    print(erase + "File {} processed".format(number))

def completed(percent):
    print("({:1.1}% Completed)".format(percent))

for i in range(1,4):
    download(i)
    completed(i/10)
    sleep(1)

Works in my python 3.4, final output is:

File 1 processed
File 2 processed
File 3 processed
(0.3% Completed)

If you want read more about terminal escape codes see: Wikipedia's ANSI escape codes article.

As requested, example with a space:

from time import sleep
erase = '\x1b[1A\x1b[2K'

def download(number):
    print(erase*2 + "File {} processed".format(number))

def completed(percent):
    print("\n({:1.1}% Completed)".format(percent))

print("\n(0.0% Completed)")
for i in range(1,5):
    download(i)
    completed(i/10)
    sleep(1)

The final output is:

File 1 processed
File 2 processed
File 3 processed
File 4 processed

(0.4% Completed)
like image 50
mucka Avatar answered Oct 22 '22 02:10

mucka


Take a look at the \r command. This could do the trick.

for i in range(2):
    print '\rFile %s processed' % i
    print '(0.%s%% Completed)' % i,

Output is:

File 0 processed
File 1 processed
(0.1% Completed)
like image 2
Nuageux Avatar answered Oct 22 '22 02:10

Nuageux