简体   繁体   中英

Python ftplib retrbinary(), files not downloading completely

Code excerpt:

file = open("D:\\DownloadFolder\\test.jpg", "wb")
def callback(data):
    file.write(data)

connect.retrbinary('RETR cover.jpg', callback)
print("completed")

This script downloads the file cover.jpg from my server. The image is returned distorted though (some parts are missing.) Any ideas?

You are writing files, but not closing them. Consider closing the file with file.close() :

file = open("D:\\DownloadFolder\\test.jpg", "wb")
def callback(data):
    file.write(data)

connect.retrbinary('RETR cover.jpg', callback)
file.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM