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
Put everything in a for loop using
os.listdir:Perhaps better would be to wrap that code in a function and then call it: