zip文件内容未显示在Windows机器上(Python的zipfile模块)

时间:2015-11-03 17:47:37

标签: python zip email-attachments

我正在将一些csv压缩成一个zip文件,以便从我的网络应用程序中通过电子邮件发送出去。

celery运行此任务,负责发送电子邮件。结构如下所述

import zipfile
...

def email_performance_report(performance_dict)
    zippath = performance_dict['folderpath'] + '.zip'
    ziph = zipfile.ZipFile(zippath, 'w')

    for root, dirs, files in os.walk(performance_dict['folderpath']):
        for file in files:
            filename = os.path.join(root, file)
            archivename = os.path.dirname(filename).split('/')[-1]
            filename_short = filename.split('/')[-1]
            ziph.write(filename, os.path.join(archivename, filename_short))

    ziph.close()

    ''' attach attachment '''
    with open(zippath, 'rb') as f:
        attachment = MIMEText(f.read())
    clean_attachment_name = zippath.split('/')[-1].split('|__|')[0] + '.zip'
    attachment.add_header('Content-Disposition', 'attachment', filename=clean_attachment_name)
    msg.attach(attachment)

    try:
            mailserver.sendmail(os.environ['MAIL_SENDER'], performance_dict['email_to'], msg.as_string())
    except smtplib.SMTPDataError:
        sqllogger.critical('A emailing of the report failed due to zippy zip issues')  # intermittent gmail issue with zip files
        return False

    os.remove(zippath)
    shutil.rmtree(performance_dict['folderpath'])

    return



@celery.task(base=SqlAlchemyTask, name='tasks.daily_performance_report')
def generate_daily_performance_report(pd):
    ... build performance dictionary ...
    email_daily_performance_report(performance_dict)

clean_attachment_name是删除了uuid的文件路径。 uuid是这样的,如果多个用户试图同时生成报告,他们就不会互相踩到。

我的用户报告空的zip文件。 zip文件夹的大小是正确的,如果他们将电子邮件转发给我,我可以在Ubuntu(Linux Mint)上打开zip文件。所有csv文件都存在。

我在这里缺少跨平台问题吗?

0 个答案:

没有答案