简体   繁体   中英

Creating a progress bar/status in Python text console

I'm coding a small Python script, that checks the records of a few domains. This is how I do it:

if results.short == True:
        isonlist = False
        for dnsbls in L:
                try:
                        if  socket.gethostbyname("%s.%s" % (ip_reversed(results.IP), dnsbls)).startswith("127"):
                                isonlist = True
                except (socket.gaierror):
                        pass
        if isonlist == True:
                print "1"
        else:
                print "0"

else:
        pass

Right now it outputs 1 if it get's a valid record and 0 if it doesn't.

Now, I'd like for it to show a progress bar, like when you use wget and the likes. Tried doing it like this:

number = number + 1

But that yields me 1 2 3 4 and so forth.

My personal favorite for this is python-progressbar . It's fast and easy to use.

Of course there are many progress bar implementation in Python. Some use curses or similar terminal libraries (example: http://nadiana.com/animated-terminal-progress-bar-in-python ), other use simple sys.stdout.write('\\rstep %d of %d' % (step, max_steps))

Notice usage of \\r that means that text you write will replace current line content on console.

Also do not use number = number + 1 , use number += 1

Giorgos Verigakis的书具有更新的,相当不错的https://github.com/verigak/progress

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