0

I have a module which is supposed to extract the email attachment and place it at a specific, The code is working fine for attachments in emails, it also works for extracting rar files stored in the pc, I think the problem is in the path of rar files to attachments

        for att in message.Attachments:
            if  att.FileName.endswith(".xlsx") :
                # Give each attachment a path and filename
                outfile_name1 = Myfolder + att.FileName
                # save file
                att.SaveASFile(outfile_name1)
            elif att.FileName.endswith("rar"):
                rf = rarfile.RarFile(att.FileName)
                rf.extractall(Myfolder)
Reda HAMZA
  • 43
  • 8

2 Answers2

0

You must correct this part of the code:

elif att.FileName.endswith("rar"):

to

elif filename.endswith(".rar") or filename.endswith(".zip"):
Salio
  • 1,058
  • 10
  • 21
  • Why would this solve the issue? anything that ends with `.rar` also ends with `rar`. And I'm not sure if the OP intended to add `.zip` files as well... – Johannes H. Apr 26 '21 at 14:38
  • Check if the string ends with the phrase @"zip" example txt = "my file is zip" but @".zip" Checks zip files. example *salio.zip* – Salio Apr 26 '21 at 16:10
  • Yes, but zip files where mentioned nowhere in the question. Just rar files. And he IS checking for those. Why would a) looking for zip files solve the issues that rar files do not get extracted? and b, how would rarfile deal with zip files? – Johannes H. Apr 27 '21 at 00:25
  • thanks, Exactly My intention rar file is like zip file – Salio Apr 27 '21 at 03:09
0

You cannot access the Rar file while it's not even stored in a file.

att.FileName is just a Stirng that holds informaiton about how the attachment was named within the email - it does not point to an actual file that exists anywhere on the file system. This implies that you cannot open an email attachment as a file (unless the function explicitely knows how to handle email files, and then it would need the email file on top of the attachment file name).

To solve your issue, store the attachment in a file first, then extract the contents of that saved file.

Something like this:

  for att in message.Attachments:
        outfile_name1 = Myfolder + att.FileName
        if  att.FileName.endswith(".xlsx") :
            # Give each attachment a path and filename
            # save file
            att.SaveASFile(outfile_name1)
        elif att.FileName.endswith("rar"):
            att.SaveASFile(outfile_name1)
            rf = rarfile.RarFile(outfile_name1)
            rf.extractall(Myfolder)

You can obviously delete the rar file again afterwards if you wish to do so.

Johannes H.
  • 5,875
  • 1
  • 20
  • 40
  • that’s what I did: `#extract rar file test = os.listdir(Myfolder) for item in test: if item.endswith(".rar"): rf = rarfile.RarFile(os.path.join(Myfolder, item)) rf.extractall(Myfolder) # Delete unused file types (like .jpg & .rar)-- for item in test: if item.endswith(".jpg") or item.endswith(".rar"): os.remove(os.path.join(Myfolder, item))` – Reda HAMZA Apr 26 '21 at 16:51
  • @RedaHAMZA Yes and that should work. If you go through all files in a directory, you deal with files, and rarfile.RarFile can open them. If, like in your question, you go through email attachments, no actual files exist, so they can't be openend either. – Johannes H. Apr 27 '21 at 00:27
  • H: Thenk's anyways – Reda HAMZA Apr 27 '21 at 21:26