首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从eml文件下载所有文档

无法从eml文件下载所有文档
EN

Stack Overflow用户
提问于 2019-11-14 19:12:34
回答 1查看 789关注 0票数 0

我有一个包含3个附件的.eml文件。我能够下载其中一个附件,但无法下载所有附件。

代码语言:javascript
复制
import os
import email
import base64
# Get list of all files
files = [f for f in os.listdir('.') if os.path.isfile(f)]
# Create output directory
if os.path.exists("output"):
    pass
else:
    os.makedirs("output")

for eml_file in files:
    if eml_file.endswith(".eml"):
        with open(eml_file) as f:
            email = f.read()

        ext=".docx"

        if ext is not "":
            # Extract the base64 encoding part of the eml file
            encoding = email.split(ext+'"')[-1]
            if encoding:
                # Remove all whitespaces
                encoding = "".join(encoding.strip().split())
                encoding = encoding.split("=", 1)[0]
                # Convert base64 to string
                if len(encoding) % 4 != 0: #check if multiple of 4
                   while len(encoding) % 4 != 0:
                       encoding = encoding + "="
                try:
                    decoded = base64.b64decode(encoding)
                except:
                    print(encoding)
                    for i in range(100):
                        print('\n')
                # Save it as docx
                path = os.path.splitext(eml_file)[0]
                if path:
                    path = os.path.join("output", path + ext)
                    try:
                        os.remove(path)
                    except OSError:
                        pass
                    with open(path, "wb") as f:
                        f.write(decoded)
        else:
            print("File not done: " + eml_file)

如何下载所有附件?编辑:我已经初始化了eml_file,但还没有下载所有文件。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-15 08:16:45

导入email模块。那么,你为什么要忽略它,自己写一个电子邮件解析器呢?此外:

  1. 您可以使用glob列出所有具有给定扩展名的文件。
  2. use应该在以下条件下使用not操作符:(if not os.path.exists("output"): os.makedirs("output")),但即使这样也没有必要,因为makedirsexist_ok参数。H 210G 211

代码语言:javascript
复制
import os
import glob
import email
from email import policy

indir = '.'
outdir = os.path.join(indir, 'output')

os.makedirs(outdir, exist_ok=True)
files = glob.glob(os.path.join(indir, '*.eml'))

for eml_file in files:
    # This will not work in Python 2
    msg = email.message_from_file(open(eml_file), policy=policy.default)
    for att in msg.iter_attachments():
        # Tabs may be added for indentation and not stripped automatically
        filename = att.get_filename().replace('\t', '')
        # Here we suppose for simplicity sake that each attachment has a valid unique filename,
        # which, generally speaking, is not true.
        with open(os.path.join(outdir, filename), 'wb') as f:
            f.write(att.get_content())
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58864183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档