Python下载大文件

用Python下载超大文件

在日常的应用中,我们有时候会用Python下载超大文件,这个时候就需要进行调整了。Just show the code。

代码如下:

1
2
3
4
5
6
r = requests.get(url, stream=True)  # 记得设置stream = True
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024): # chunk_size 每次下载1024个字节
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush() # 刷新缓冲区




Just Have Fun…