gpt4 book ai didi

python - 如何检查要完成下载 Python3 的文件?

转载 作者:太空宇宙 更新时间:2023-11-04 09:40:03 26 4
gpt4 key购买 nike

我想弄清楚如何检查下载是否已完成。基本上我希望它等到文件下载完成然后打印:下载完成。

这是我当前的代码以及我想用它做什么:

from  urllib import request

print("Are you sure you want to download the newest file? y/n")
answer = input()

while True:
if answer == 'y':
print("Downloading file...")
downloading = True
request.urlretrieve("FILE_URL", "FILE_NAME")
elif answer == 'n':
exit()
else:
print("That is not a valid answer, please answer with y/n.")
answer = input()

#I need some sort of function here that checks if the file is still being
#downloaded

when downloading == False:
print("Download Completed.")

最佳答案

urlretrieve 返回时,文件已经下载完成。

请参阅 docs 中的用法示例:

>>> import urllib.request
>>> local_filename, headers = urllib.request.urlretrieve('http://python.org/')
>>> html = open(local_filename)

可以看出,文件在调用 urlretrieve 后立即打开,因为它已经创建并且内容已经写入那里。

关于python - 如何检查要完成下载 Python3 的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52070906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com