简体   繁体   English

在Python文本控制台中创建进度条/状态

[英]Creating a progress bar/status in Python text console

I'm coding a small Python script, that checks the records of a few domains. 我正在编写一个小的Python脚本,该脚本检查一些域的记录。 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. 现在,如果得到有效记录,则输出1,否则得到0。

Now, I'd like for it to show a progress bar, like when you use wget and the likes. 现在,我希望它显示进度条,就像您使用wget之类的。 Tried doing it like this: 尝试这样做:

number = number + 1

But that yields me 1 2 3 4 and so forth. 但这使我得到1 2 3 4,依此类推。

My personal favorite for this is python-progressbar . 我个人最喜欢的是python-progressbar It's fast and easy to use. 它快速且易于使用。

Of course there are many progress bar implementation in Python. 当然,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)) 一些使用curses或类似的终端库(例如: http : sys.stdout.write('\\rstep %d of %d' % (step, max_steps)) ),其他一些使用简单的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. 注意\\r用法,这意味着您编写的文本将替换控制台上的当前行内容。

Also do not use number = number + 1 , use number += 1 也不要使用number = number + 1 ,使用number += 1

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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