pythonパスのすべてのメールの送信者と受信者2.0を読み込む

2062 ワード

import os
import re
import email
import time

#Traverse the files
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)

#rt
def GetEmailInfo(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]
            listTo = (msg.get("to")).split(',')
            for to in listTo:
                toTemp = email.utils.parseaddr(to)[1]
                fto.write(str(toTemp)+'
') ffrom.write(str(peopleFrom)+'
') except: pass if __name__ == "__main__": #input the disc path path = raw_input('input the path of the email: ') #open file to write ffrom = open('from.txt','a') fto = open('to.txt','a') ffrom.write('added at:'+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+'
') fto.write('added at:'+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+'
') ffrom.write('-------------------------------------------------------------------------
') fto.write('-------------------------------------------------------------------------
') #Traverse the emails WalkDir(path, GetEmailInfo) ffrom.write('*************************************************************************
') fto.write('*************************************************************************
') ffrom.close() fto.close()

  
転載先:https://www.cnblogs.com/chhyong88/archive/2011/12/01/2271148.html