如何在Python3中检查文件是否已经下载完成?

3

我正在尝试弄清楚如何检查下载是否已完成。基本上,我希望它等待文件下载完成,然后打印:下载完成。

以下是我的当前代码以及我想要的操作:

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.")

3
while answer is not 'y' or 'n':并不像你想象的那样工作。请参阅如何测试多个变量是否等于一个值?进行了解。我会将这部分内容编辑掉,因为与问题无关。 - Aran-Fey
它虽然有效。 - NeverEndingCycle
1
@VineethSai 文件存在并不意味着下载已经完成。 - Aran-Fey
啊,忘记了那个临时文件。 - Vineeth Sai
1
这就是整个问题所在。当下载一个大文件时,我需要知道何时完成。我之前发布了一个问题,说我的代码不起作用,但实际上下载还没有完成,浪费了大家的时间。我需要知道什么时候可以安全地关闭程序并打开文件。 - NeverEndingCycle
显示剩余5条评论
2个回答

4

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

请参考文档中的使用示例:

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

正如你所看到的,urlretrieve调用后立即打开文件,因为它已经被创建并且内容已经写入其中。

那么我该如何将这个实现到我的代码中呢?我需要一些东西来将“downloading == True”更改为“downloading == False”。 - NeverEndingCycle
1
@NeverEndingCycle 关键是你不需要那些。下载已经完成了。urlretrieve(...)调用之后添加print("下载完成。")即可。 - Aran-Fey

2
如果您使用 urllib.request.urlretrievereporthook 选项,您可以监视下载进度,尤其是对于大文件非常有用,如下所示:
import urllib.request

def Download_Progress(block_num, block_size, total_size):
    downloaded = block_num * block_size
    progress = int((downloaded/total_size)*100)
    print ("Download Progress",str(progress),"%")

url = "https://somesite/some.pdf"
urllib.request.urlretrieve(url, 'mycopy.pdf', reporthook=Download_Progress)
print ("Finished")

您也可以使用requests包来实现类似的功能。
import requests
url = "https://somesite/some.pdf"

#Get the headers of the remote file
h = requests.head(url, allow_redirects=True)

#Get the size of the file
total_size = int(h.headers.get('content-length'))

#Request the file download with stream set to True
r = requests.get(url, stream=True)

#Open a local file for writing
localfile = open("mycopy.pdf", "wb")
chunks = 0

#Process the file as it arrives
for chunk in r.iter_content(chunk_size=512):
    if chunk:
        chunks += 1
        downloaded = chunks * 512
        # An approximation as the chunks don't have to be 512 bytes
        progress = int((downloaded/total_size)*100)
        print ("Download Progress",str(progress),"%")
        localfile.write(chunk)
print("Finished")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接