讀取檔案大小-列印進度條

jhchena發表於2024-07-24
點選檢視程式碼
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='')

相關文章