在多个文件上运行python脚本以提取消息

时间:2016-07-04 09:37:48

标签: python email ole extraction

我在网上找到了一个方便的python脚本,允许我从.msg文件中提取文本和附件(从outlook保存的电子邮件)

- 链接到脚本:https://github.com/mattgwwalker/msg-extractor

如果我从cmd运行脚本,我需要编写正在使用的文件名:

Python msgextractor.py filename.msg

我需要从单个文件夹中提取许多文件的附件 所以我不知何故需要python循环遍历文件夹中的所有.msg文件并运行脚本。 (最好来自第二个.py文件)

我会感谢所有的帮助和提示!

2 个答案:

答案 0 :(得分:0)

尝试使用glob。它允许您轻松迭代文件。

import glob
files = glob.glob('folder/*.msg')
for file in files:
    # whatever you do to individual files goes here

答案 1 :(得分:0)

这是我在底部的脚本中使用/修改的代码

if __name__ == "__main__":
    writeRaw = False
    toJson = False
    useFileName = False
    for root, dirs, files in os.walk('D:\\Sync\\Images\\'):
        for file in files:
            if file.endswith(".msg"):
                print file
                savePath =  os.path.join(root) 
                for filename in glob.glob(file):
                    msg = Message(filename,savePath)
                    print 'boom'
                    msg.save(savePath, toJson, useFileName)
相关问题