python 读取某一路径所有邮件的寄件人和收件人

import os
import email
 
def WalkDir(dir, file_callback=None):
    i=0;
    for root, dirs, files in os.walk(dir):
        for f in files:
            i=i+1
            print "prosessing the email:"+f
            file_path = os.path.join(root, f)
            if file_callback:
                file_callback(file_path)
 
def GetSenderInfo(file):
    try:
        ext = os.path.splitext(file)[1]
        if  ext in ('.EML', '.eml'):
            femail = open(file)
            msg = email.message_from_file(femail)
            peopleFrom = email.utils.parseaddr(msg.get("from"))[1]
            peopleTo = email.utils.parseaddr(msg.get("to"))[1]
            ffrom.write(str(peopleFrom)+'\n')
            fto.write(str(peopleTo)+'\n')
            femail.close()

    except:
        pass
 
if __name__ == "__main__":
    path = raw_input('Path: ')
    ffrom = open('from.txt','w')
    fto = open('to.txt','w')
    WalkDir(path, GetSenderInfo)
    ffrom.close()
    fto.close()

  

转载于:https://www.cnblogs.com/chhyong88/archive/2011/12/01/2270865.html

你可能感兴趣的:(python 读取某一路径所有邮件的寄件人和收件人)