【发布时间】:2018-08-29 06:32:25
【问题描述】:
我正在尝试弄清楚如何检查下载是否已完成。基本上我希望它等到文件完成下载然后打印:下载完成。
这是我当前的代码以及我想用它做什么:
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.")
【问题讨论】:
-
while answer is not 'y' or 'n':不会像您认为的那样做。见How to test multiple variables against a value?。我会编辑它,因为它与问题无关。 -
它可以工作。
-
巧合的是,是的。实际上,我刚刚破解了你的密码……等等……我们走了。
-
@VineethSai 文件存在并不意味着下载完成。
-
这就是整个问题。下载大文件时,我需要知道它何时完成。我早些时候发布了一个问题,说我的代码不起作用,但实际上下载还没有完成,我浪费了大家的时间。我需要知道何时可以安全地关闭程序并打开文件。
标签: python python-3.x