03 读取文件 显示进度条

点击查看代码
import os

# 1. 读取文件大小(字节)
file_size = os.stat('20190409_192149.mp4').st_size

# 2.一点一点的读取文件
read_size = 0
with open('20190409_192149.mp4',mode='rb') as f1,open('a.mp4',mode='wb') as f2:
    while read_size < file_size:
        chunk = f1.read(1024) # 每次最多去读取1024字节
        f2.write(chunk)
        read_size += len(chunk)
        val = int(read_size / file_size * 100)
        print('%s%%\r' %val ,end='')
posted @ 2024-07-24 21:51  jhchena  阅读(10)  评论(0编辑  收藏  举报