python用tqdm库来实现进度条功能
来源:互联网 发布:网络电商招商 编辑:程序博客网 时间:2024/05/21 22:30
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:
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 mmapdef 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用tqdm库来实现进度条功能
- python 进度条 tqdm
- Python进度条(tqdm)
- tqdm【Python进度条】
- Python 进度条 tqdm
- python tqdm进度条模块
- python进度条 tqdm 解析
- 安装tqdm-python的显示程序运行进度条库
- tqdm==>python可扩展进度条
- tqdm==>python可扩展进度条
- 好玩的Python库tqdm
- python的Tqdm模块
- 进度条样式用渐变来实现
- php用ob_flush实现进度条功能
- java实现进度条功能
- python包tqdm安装及入门
- Python实现进度条总结
- python实现进度条
- java IO流 之 字符流之FileWriter
- Java程序员的成长书单
- ts packet结构分析
- win 10和VM ubuntu16.04实现复制与粘贴
- 64. Minimum Path Sum
- python用tqdm库来实现进度条功能
- 前端面试题
- java6
- html事件
- 反转单向和双向链表简化
- 编译那些事
- JVM笔记二
- NULL ,nullptr,nullptr_t,0 区别
- C语言基础之字符串(附测试代码)