سؤال

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()
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top