Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9240415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:11:10+00:00 2026-06-18T08:11:10+00:00

I am quite new to Python and I have created some simple Python code

  • 0

I am quite new to Python and I have created some simple Python code (below) to print (or return) the body, subject and from email address of a .eml file. This works fine. However, how can i apply this code to a folder of .eml files?

import email

fp = open('C:\emlfiles\TEST_00000.eml')

msg = email.message_from_file(fp)

subject = msg.get('subject')
subject = str(subject)
fromAdd = msg.get('from')
fromAdd = str(fromAdd)
payload = msg.get_payload()
if type(payload) == type(list()) :
    payload = payload[0] 
if type(payload) != type('') :
    payload = str(payload)

print subhec + fromAdd + payload
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:11:11+00:00Added an answer on June 18, 2026 at 8:11 am

    Put everything in a for loop using os.listdir:

    import email
    
    infolder = "C:\\emlfiles\\"
    for infile in os.listdir(infolder):
        fp = open(os.path.join(infolder, infile))
        msg = email.message_from_file(fp)
        # etc
    

    Perhaps better would be to wrap that code in a function and then call it:

    def process_email(infile):
        fp = open(infile)
        msg = email.message_from_file(fp)
        subject = msg.get('subject')
    
    infolder = "C:\\emlfiles\\"
    for infile in os.listdir(infolder):
        process_email(os.path.join(infolder, infile))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to interact with a simple .exe I created from some Python
I'm creating a game, and am quite new to Python generally. I created a
I am quite new to mongoid and rails. So I have some troubles to
I'm quite new to threading in Python and have a couple of beginner questions.
I am quite new in python.I want to match string in some lines of
I am writing some Python code to implement some of the concepts I have
I'm quite new to programming, but I've learned some basic Python programming in a
I'm quite new to Python and currently I need to have a __repr__ for
I'm quite new to regular expressions in Python. I have the following string and
I have a method in c++ that gets called from python and needs to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.