python批量提取eml附件

从批量eml文件中提取附件,使用方式如下

 

代码如下

 1 import email
 2 import os
 3 import sys
 4 
 5 #获取eml附件信息
 6 def Get_Annex_Message(FilePath, Annex_Path):
 7     global sum
 8     try:
 9         fp = open(FilePath, 'rb')        #打开任意格式文件,通过email库来判断是否为eml文件
10         msg = email.message_from_binary_file(fp)
11         for part in msg.walk():            #循环信件中的每一个mime的数据块
12             if part.get_content_maintype() == 'multipart':
13                 continue
14             Annex_name = part.get_filename()
15             if Annex_name:        #如果附件存在名字
16                 Annex_name = str(sum) + '.' + Annex_name
17                 fp = open(os.path.join(Annex_Path, Annex_name), 'wb')
18                 fp.write(part.get_payload(decode=True))
19                 sum += 1
20                 
21     except Exception as e:
22         print(e)
23         return
24         
25 #递归文件夹下所有文件
26 def List_Filepath(Eml_Path, Annex_Path):
27     for parent,dirnames,filenames in os.walk(Eml_Path): #遍历文件夹
28         for dirname in dirnames:        #对文件夹进行递归
29             List_Filepath(dirname, Annex_Path)
30         for filename in filenames:        #r对文件进行判断
31             FilePath = os.path.join(parent,filename)
32             Get_Annex_Message(FilePath, Annex_Path)
33         
34 #创建目的文件夹
35 def Create_Dir(Annex_Path):
36     if os.path.exists(Annex_Path):
37         print("dir exists, Annex file will create in %s" % Annex_Path)
38     else:
39         os.mkdir(Annex_Path)
40         
41 #主函数
42 def main():
43     global sum 
44     sum = int(1)
45     Eml_Path = sys.argv[1]        #第一个参数为eml所在文件夹
46     Annex_Path = sys.argv[2]    #第二个参数为eml附件输出的路径
47     Create_Dir(Annex_Path)        #创建保存附加的文件夹
48     List_Filepath(Eml_Path, Annex_Path)        
49 
50 if __name__ == "__main__":
51     main()
52     

 

posted @ 2019-04-26 22:41  QKSword  阅读(5563)  评论(4编辑  收藏  举报