Python_下载大文件校验

2020-02-28  本文已影响0人  古佛青灯度流年

1.下载文件

requests.adapters.DEFAULT_RETRIES = 5
response = requests.get(url, stream=True)
status = response.status_code
if status == 200:
    total_size = int(response.headers['Content-Length'])
    with open('xxx', 'wb') as of:
        for chunk in response.iter_content(chunk_size=102400): # 块状下载,节省内存空间
            if chunk:
                of.write(chunk)

2.校验下载完成文件与原始文件大小。

with open('xxx', 'r') as f:
    if isinstance(f, file):
        length = os.fstat(f.fileno()).st_size
        
if total_size == length:
    True
else:
    False
上一篇下一篇

猜你喜欢

热点阅读