output to the same line overwriting previous outpu

2019-01-01 15:15发布

I am writing a simple ftp downloader. Part of to the code is something like this:

ftp.retrbinary("RETR " + file_name, process)

i am calling function process to handle the callback:

def process(data):
    print os.path.getsize(file_name)/1024, 'KB / ', size, 'KB downloaded!'
    file.write(data)

and output is something like this:

1784  KB / KB 1829 downloaded!
1788  KB / KB 1829 downloaded!
etc...   

but i want it to print this line and next time reprint/refresh it so it will only show it once and i will see progress of that download...

How can it be done?

7条回答
余生请多指教
2楼-- · 2019-01-01 16:02

Have a look at the curses module documentation and the curses module HOWTO.

Really basic example:

import time
import curses

stdscr = curses.initscr()

stdscr.addstr(0, 0, "Hello")
stdscr.refresh()

time.sleep(1)

stdscr.addstr(0, 0, "World! (with curses)")
stdscr.refresh()
查看更多
登录 后发表回答