How do I write output in same place on the console?

202,496

Solution 1

You can also use the carriage return:

sys.stdout.write("Download progress: %d%%   \r" % (progress) )
sys.stdout.flush()

Solution 2

Python 2

I like the following:

print 'Downloading File FooFile.txt [%d%%]\r'%i,

Demo:

import time

for i in range(100):
    time.sleep(0.1)
    print 'Downloading File FooFile.txt [%d%%]\r'%i,

Python 3

print('Downloading File FooFile.txt [%d%%]\r'%i, end="")

Demo:

import time

for i in range(100):
    time.sleep(0.1)
    print('Downloading File FooFile.txt [%d%%]\r'%i, end="")

PyCharm Debugger Console with Python 3

# On PyCharm Debugger console, \r needs to come before the text.
# Otherwise, the text may not appear at all, or appear inconsistently.
# tested on PyCharm 2019.3, Python 3.6

import time

print('Start.')
for i in range(100):
    time.sleep(0.02)
    print('\rDownloading File FooFile.txt [%d%%]'%i, end="")
print('\nDone.')

Solution 3

Use a terminal-handling library like the curses module:

The curses module provides an interface to the curses library, the de-facto standard for portable advanced terminal handling.

Solution 4

Print the backspace character \b several times, and then overwrite the old number with the new number.

Solution 5

For Python 3xx:

import time
for i in range(10):
    time.sleep(0.2) 
    print ("\r Loading... {}".format(i)+str(i), end="")
Share:
202,496

Related videos on Youtube

scottm
Author by

scottm

Software engineer with skills in highly available systems and software solutions.

Updated on June 21, 2021

Comments

  • scottm
    scottm about 3 years

    I am new to python and am writing some scripts to automate downloading files from FTP servers, etc. I want to show the progress of the download, but I want it to stay in the same position, such as:

    output:

    Downloading File FooFile.txt [47%]

    I'm trying to avoid something like this:

         Downloading File FooFile.txt [47%]
         Downloading File FooFile.txt [48%]
         Downloading File FooFile.txt [49%]
    

    How should I go about doing this?


    Duplicate: How can I print over the current line in a command line application?

  • Chris Ballance
    Chris Ballance over 15 years
    interesting, I hadn't thought of doing it that way.
  • ephemient
    ephemient over 15 years
    Very common and simple solution. Note: if your line is longer than the width of your terminal, this gets ugly.
  • scottm
    scottm over 15 years
    I also had to add a call to sys.stdout.flush() so the cursor didn't bounce around
  • Diego Herranz
    Diego Herranz about 11 years
    Not available for Windows.
  • Nathan Donnellan
    Nathan Donnellan about 11 years
    I like this because it doesn't clear previous commands (if you have multiple stages you want to leave on the screen)
  • Plexico
    Plexico over 10 years
    @Diego there's now a support library for curses module on Windows. see stackoverflow.com/a/19851287/1426237
  • cod3monk3y
    cod3monk3y over 10 years
    Using carriage return (e.g. print 'Downloading.... \r') also doesn't clear previous data, but it prevents having to know how far back to back up.
  • EarlCrapstone
    EarlCrapstone about 10 years
    Is it possible to do this with multiple lines? Lets say I have three different downloads, and I want to show the progress of each one on its own line.
  • hkoosha
    hkoosha about 10 years
    use this for python 3+: print('Downloading File FooFile.txt [%d%%]\r'%i, end="")
  • augurar
    augurar over 9 years
    I like to put the \r at the beginning of the line, and add a \x1b[K to clear the previous text.
  • Ogen
    Ogen about 8 years
    What if I have multiple lines? For example, multiple downloads happening at the same time. It doesn't seem to work when I just add newlines, it prints infinitely. I.e., sys.stdout.write("Download progress: %d%% \n\r" % (progress) ) doesnt work
  • Joshua Grosso Reinstate CMs
    Joshua Grosso Reinstate CMs over 7 years
    Why is this better than the above (I'm a Python n00b, so please excuse my ignorance :-))?
  • PM 2Ring
    PM 2Ring over 6 years
    In modern Python, you can supply an arg of flush=True to print, so there's no need for the extra sys.stdout.flush() call.
  • Cyrus
    Cyrus over 6 years
    It seems like the simplest solution for python 3 (as mentioned in answers below) is: print("sample text", end='\r", flush=True)
  • Yul Kang
    Yul Kang over 4 years
    On PyCharm Debugger console, \r needs to come before the text. Otherwise, the text may not appear at all, or appear inconsistently. I added the version that works for me as an edit, because I couldn't write multi-line code in this answer. I put it on my gist so people can view it while the edit is awaiting approval: gist.github.com/yulkang/40168c7729a7a7b96d0116d8b1bc26df
  • battey
    battey about 4 years
    "\r" at the end of the string works for me in the debugger console on PyCharm 2020.1 (PyCharm 2020.1.2 (Community Edition); Build #PC-201.7846.77, built on May 31, 2020).
  • codesnerd
    codesnerd over 3 years
    @Cyrus that worked seamlessly. Just fix the quotes with \r :)
  • Dave
    Dave about 3 years
    First example has "sys.sdtout" gives a syntax error. Change to "sys.stdout".