TeamMsgExtractor / msg-extractor

Extracts emails and attachments saved in Microsoft Outlook's .msg files
GNU General Public License v3.0
728 stars 171 forks source link

Email not closed after error and os.remove failed #424

Closed ragebear00 closed 1 month ago

ragebear00 commented 1 month ago

Bug Metadata

Describe the bug Email not closed after error and os.remove failed

Traceback (most recent call last): File "C:/Extract_Msg.py", line 73, in print(ErrorTest(r"C:_a\18.msg")) File "C:/Extract_Msg.py", line 37, in ErrorTest msg.saveAttachments(customPath=r'C:_a') File "C:\Users\z\AppData\Local\Programs\Python\Python310\lib\site-packages\extract_msg\msg_classes\msg.py", line 808, in saveAttachments attachment.save(skipHidden = skipHidden, kwargs) File "C:\Users\z\AppData\Local\Programs\Python\Python310\lib\site-packages\extract_msg\attachments\emb_msg_att.py", line 121, in save return self.data.save(kwargs) File "C:\Users\z\AppData\Local\Programs\Python\Python310\lib\site-packages\extract_msg\msg_classes\message_base.py", line 792, in save raise DataNotFoundError('Plain text body could not be found.') extract_msg.exceptions.DataNotFoundError: Plain text body could not be found.

Add the following can close the email and os.remove succeeds

def msgAttachment(filename):

~ time.sleep(1)

nAttachment = 0
try:
    msg = extract_msg.openMsg(filename)      #filename including path
    msg.saveAttachments(customPath=r'C:\_a')
except Exception as e:
    print("=================================Error Extract_Msg Attachment==================")
    msg.close()
    msg = extract_msg.openMsg(filename)
    meta = msg.body
    msg.close()
    os.remove(filename)
TheElementalOfDestruction commented 1 month ago

I'll see if I can replicate the issue you're experiencing, but I can't really think of a reason for it to be going wrong except that maybe your python interpreter isn't actually releasing the file handles when they are closed (something I don't think I'd be able to fix).

In the meantime, can you tell me if replacing this in the second code segment:

msg = extract_msg.openMsg(filename)
meta = msg.body
msg.close()

with

del msg

would also resolve the issue for you? If so, then it would suggest that a reference attached to the msg file is not getting garbage collected and is what is breaking things.

ragebear00 commented 1 month ago

try del msg, but does not work.

However, I test the same code in a different computer (same version number), msg.close() can close the file in the exception process (after error raised) and os.remove successfully.

The error may be caused by something else. At present, let's assume it is solved.

TheElementalOfDestruction commented 1 month ago

My best guess then is that python is not actually releasing the file handle on that machine when close is called, for whatever reason.