1
我目前使用此代碼打印出使用ftp.retrbinary從ftp服務器下載的百分比,但下載完成時爲0.27%完成,並且只能說明sizeWritten以27828224位結束。我哪裏錯了?如何跟蹤使用ftp.retrbinary下載的百分比?
from ftplib import FTP
ftp = FTP('host')
ftp.login('usr','pass')
totalSize = ftp.size('100file.zip')
print(totalSize, "bytes")
def download_file(block):
global sizeWritten
file.write(block)
sizeWritten += 1024
print(sizeWritten, "= size written", totalSize, "= total size")
percentComplete = sizeWritten/totalSize
print (percentComplete, "percent complete")
try:
file = open('100file.zip', "wb")
ftp.retrbinary("RETR " + '100file.zip' ,download_file)
print("Download Successful!")
except:
print("Error")
file.close()
ftp.close()
你怎麼知道每個塊的長度是1024字節? – Hyperboreus
作爲一個方面說明,你似乎混合了比特和字節,當你的數字是4倍數或者是1/2倍時,這幾乎不可能提供幫助。 – abarnert