python用tqdm库来实现进度条功能
2017-10-29 16:20
507 查看
I’ve been a fan of the tqdm Python module for quite some time, but I found it difficult to find a reason to use it; generally, loops run fast enough that a progress bar is unnecessary. However, I found a perfect use for it in reading large files.
If the task isn’t something I can speed up via multiprocessing, I can use tqdm to decide whether I can grab a cup of coffee or work on something else while I let it run. tqdm allows me to easily add a progress bar to the read operation, like so:
As you can see, adding this functionality is as simple as wrapping the file with the tqdm method. However, to display the progress bar, tqdm needs an idea of how many total lines it needs to process. I use this code snippet from StackOverflow to quickly find this information when instantiating the progress bar:
Here’s what it looks like in action:
Pretty neat, in my opinion!
If you have any questions, comments, or suggestions, you’re welcome to leave a comment below.
If the task isn’t something I can speed up via multiprocessing, I can use tqdm to decide whether I can grab a cup of coffee or work on something else while I let it run. tqdm allows me to easily add a progress bar to the read operation, like so:
with open(file_path) as file: for line in tqdm(file, total=get_num_lines(file_path)): # various operations here
As you can see, adding this functionality is as simple as wrapping the file with the tqdm method. However, to display the progress bar, tqdm needs an idea of how many total lines it needs to process. I use this code snippet from StackOverflow to quickly find this information when instantiating the progress bar:
import mmap def get_num_lines(file_path): fp = open(file_path, "r+") buf = mmap.mmap(fp.fileno(), 0) lines = 0 while buf.readline(): lines += 1 return lines
Here’s what it looks like in action:
Pretty neat, in my opinion!
If you have any questions, comments, or suggestions, you’re welcome to leave a comment below.
相关文章推荐
- python控制台中实现进度条功能
- python控制台中实现进度条功能
- Python使用progressbar模块实现的显示进度条功能
- Python实现控制台进度条功能
- python之Tqdm库实现进度条功能
- Python实现控制台进度条功能
- python实现类似awk的简单功能
- 【Python/PE】python中实现文件操作的几种功能(删除指定目录下的所有内容,文件复制等)
- Python模拟登陆5:实现图书馆预约功能
- ElasticSearch scan和scroll功能 python 实现
- Python 使用SMTP 实现发邮件功能
- Python操作mysql数据库实现增删查改功能的方法
- Python实现“各类型文件统计”功能
- [Python]实现XMPP协议即时通讯发送消息功能
- Python实现mysql数据库更新表数据接口的功能
- Python 使用with上下文实现计时功能
- Python django实现简单的邮件系统发送邮件功能
- Python实现基于多线程、多用户的FTP服务器与客户端功能完整实例
- Python 实现12306登录功能实例代码
- python实现软件的注册功能(机器码+注册码机制)