Python Forum
Trying to determine attachment file type before saving off..
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to determine attachment file type before saving off..
#1
So i have the following code in order to save email attachments from emails within a folder.. and cant seem to figure out how to check the attachment type before i download it..

so my whole logic works great, but yesterday i ran into situations where there were emails in the folder (#1 that shouldn't be in there) that had more than 2 attachments, but they were something other than CSV's, so i would like to check the attachment type before i just assume its the files i need to download.

I found a few examples, that use content_type and get_content_type() and stuff like that, but none of those work or even exist for my attachment class im using below..

Im using Spyder(python 3.9) to write and run this, is there any shortcut to present me with all available properties for a class? Like intellisense that is in Visual Studio? (all preferences are enabled and work sometimes, but not sure if it works because those classes have something to show and what im trying does not)

Right now, if there are 3 files and all of them are say jpg, it will download the first 2 and i dont need or want that..

    # THIS ENSURES THAT THE EMAIL HAS AT LEAST 2 ATTACHMENTS BEFORE IT TRIES TO DOWNLOAD FILES THAT DONT EXIST
    # NEED TO FIND A WAY TO ONLY CARE IF THE ATTACHMENTS ARE CSV FILES AND NOTHING ELSE.
    if attachments.count > 2:

        attachment1 = attachments.Item(1)
        attachment_name1 = str(attachment1).lower()
        p1 = Path(attachment_name1)
        newName1 = "{0}_{1}{2}".format(p1.stem,dtappend,p1.suffix)
        
        attachment1.SaveASFile("F:\Attachments"+ '\\' + newName1)
        
        attachment2 = attachments.Item(2)
        attachment_name2 = str(attachment2).lower()
        p2 = Path(attachment_name2)
        newName2 = "{0}_{1}{2}".format(p2.stem,dtappend,p2.suffix)
        
        attachment2.SaveASFile("F:\Attachments"+ '\\' + newName2)
Reply
#2
Found a solution that seems to work and do the job..

    # THIS ENSURES THAT THE EMAIL HAS AT LEAST 2 ATTACHMENTS BEFORE IT TRIES TO DOWNLOAD FILES THAT DONT EXIST
    # NEED TO FIND A WAY TO ONLY CARE IF THE ATTACHMENTS ARE CSV FILES AND NOTHING ELSE.
    if attachments.count >= 2:

        for att in attachments:
            p = Path(str(att).lower())
            newName = "{0}_{1}{2}".format(p.stem,dtappend,p.suffix)
            if p.suffix == ".csv":
                att.SaveASFile("F:\attachments"+ '\\' + newName)   
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to download TLS Report attachment blason16 6 555 Feb-26-2024, 07:36 AM
Last Post: Pedroski55
  How does sqlite3 module determine value type mirlos 2 955 Dec-12-2023, 09:37 AM
Last Post: mirlos
  Extract PDF Attachment from Gmail jstaffon 0 588 Sep-10-2023, 01:55 PM
Last Post: jstaffon
  determine parameter type in definition function akbarza 1 594 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  Saving the times a script is run to a file or ... 3Pinter 7 1,412 Oct-19-2022, 05:38 PM
Last Post: 3Pinter
  Code Assistance needed in saving the file MithunT 0 821 Oct-09-2022, 03:50 PM
Last Post: MithunT
  Saving the print result in a text file Calli 8 1,813 Sep-25-2022, 06:38 PM
Last Post: snippsat
  I get attachment paperclip on email without any attachments monika_v 5 2,004 Mar-19-2022, 10:20 PM
Last Post: cosmarchy
  Showing and saving the output of a python file run through bash Rim 3 2,478 Oct-06-2021, 10:48 AM
Last Post: gerpark
  File type (mode) rayleiter 5 2,172 Aug-29-2021, 07:46 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020