如下代码,是我写的一段下载谷歌驱动的方法。
其中需要下载内容已经处理,并写入driver_download_list.txt。可正常读取,下载目录也没问题。
但是执行的时候下载的文件总是不全,只有86kb 左右。
同样的方法如果不用线程池或for循环 批量下载就可以正常完整下载。
@staticmethod
def download(path, url):
"""下载方法,path为包含文件名的文件路径,需要带上后缀名如.zip"""
print(f"准备下载 {url}")
with open(path, mode="wb") as f:
f.write(requests.get(url).content)
@staticmethod
def down_pool_main():
with open("./driver_download_list.txt", mode="r", encoding="utf-8") as file:
with ThreadPoolExecutor(max_workers=5) as pool:
for version_line in file.readlines():
path, url = version_line.split("===")
pool.submit(Driver_Download.download, path, url)
pool.shutdown(wait=True)
直接指定path和url单个下载就没问题,但是一旦用了循环或者线程池就有问题,下载位置以及文件名都对着,但是文件不完整。
尝试过网上说的 requests流式下载,也是没作用。
你写的方法错了,w模式会覆盖前面的内容,要用a
with open(path, mode="a") as f:
a 就是append的意思,追加内容,会保留之前的