python分片下载文件
分片下载文件
import os os.makedirs('../data/materials/mp3/', exist_ok=True) File_PATH = '../data/materials/' def chunkDownload(url,dType): import requests r = requests.get(url, stream=True) fileName = os.path.basename(url) savePath = File_PATH + '' +dType+'/'+fileName with open(savePath, 'wb') as f: for chunk in r.iter_content(chunk_size=32): f.write(chunk) print('download complete ------' + fileName) FILE_URL = "https://XXXXX/ta1.mp3" print(chunkDownload(FILE_URL,'mp3'))
chunk_size 指定每次写入大小