您当前的位置: 首页 >  Python

Xavier Jiezou

暂无认证

  • 0浏览

    0关注

    394博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Python】读取大文件时显示彩色进度条(rich)

Xavier Jiezou 发布时间:2022-05-29 21:19:00 ,浏览量:0

引言

为了更友好的用户体验,我们在读取大文件时可以制作一个进度条,以展示文件读取进度,本文是其基于 rich 库的 Python 实现。

安装
pip install rich
样例

本文以 900MB 左右的 .tar.gz 压缩文件为例,展示其在读取时显示进度条的实现方法。

方法 使用默认进度条样式
import tarfile

import rich.progress

with rich.progress.open("test.tar.gz", "rb") as file:
    with tarfile.open(fileobj=file) as tar:
        for tarinfo in tar:
            pass

在这里插入图片描述

自定义进度条的样式
import tarfile

from rich.progress import Progress
from rich.progress import (
    BarColumn,
    DownloadColumn,
    Progress,
    SpinnerColumn,
    TaskProgressColumn,
    TimeElapsedColumn,
    TimeRemainingColumn,
)

progress = Progress(
    SpinnerColumn(),
    "{task.description}",
    BarColumn(),
    DownloadColumn(),
    TaskProgressColumn(),
    TimeElapsedColumn(),
    TimeRemainingColumn(),
)

with progress:
    with progress.open("test.tar.gz", "rb", description="Extracting...") as file:
        with tarfile.open(fileobj=file) as tar:
            for tarinfo in tar:
                pass

在这里插入图片描述

参考

Rich 12.4.4 documentation / Progress Display / Reading from a file

关注
打赏
1661408149
查看更多评论
立即登录/注册

微信扫码登录

0.0369s