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

时间:2018-08-29 06:32:26

标签: python python-3.x

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

这是我当前的代码以及我要使用的代码:

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

2 个答案:

答案 0 :(得分:2)

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

请参见docs中的用法示例:

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

可以看到,在调用urlretrieve之后,文件已经被创建并且内容已经写入其中,因此会立即打开。

答案 1 :(得分:0)

如果使用urllib.request.urlretrieve reporthook选项,则可以监视下载进度,对于大文件尤其有用,例如:

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