简体   繁体   中英

How do I update output to the command line without printing to a new line?

I'd like to do things like make updating progress bars and realtime counts of files processed, but I don't want it to span hundreds of line.

Specifically, I'm processing about 6500 emails and it takes several minutes. I'd like to print how many have been processed without it taking 6500 lines.

I've seen some solutions in Python 2, but I haven't been able to figure it out in Python 3.

You should print it using sys.stdout with carriage return '\\r' . Here you have an example:

import time
import sys

for i in range(100):
    sys.stdout.write(str(i+1) + '%\r')
    sys.stdout.flush()
    time.sleep(0.1)

Try it out!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM