I fixed my own issue. I needed to be opening the file in binary mode as well.
So
with open(fileName, 'w') as f:
becomes
with open(fileName, 'wb') as f:
سؤال
I have an email that I am trying to download the attachment from, using imaplib.
I am using the Email library to translate messages to the email.message class. This is working perfectly for all of the emails I am dealing with, but when I fetch this attachment, the file looks intact, and using Notepad++'s compare, matches the original cab file downloaded through Thunderbird.
This is an example of the code I am using.
fileContents = message.get_payload()[1].get_payload(decode=True) #
fileName = message.get_payload()[1].get_filename()
with open(fileName, 'w') as f:
f.write(fileContents)
As I said, the file contents appear identical to the original file downloaded through other clients. It has the same size, precisely. However, when I open the downloaded file in WinRar, 7zip or using Window's EXPAND.exe, it gives the following message.
! H:\Code\Backup Monitor\backupsite\tst.CAB: The archive is corrupt
Does anyone have any idea why this might be happening?
المحلول
I fixed my own issue. I needed to be opening the file in binary mode as well.
So
with open(fileName, 'w') as f:
becomes
with open(fileName, 'wb') as f: