目录列表没有换行符

时间:2017-03-26 21:09:26

标签: python list

此脚本列出了列出到我的个人电子邮件的目录和电子邮件的内容。这一切都运行正常,但问题是它填充了一个巨大的文本墙,而不是按照应有的方式打破每个文件列表。我究竟做错了什么?

import os
import smtplib
import platform
from datetime import datetime

def creation_date(path_to_file):

    if platform.system() == 'Windows':
        files = os.listdir(path_to_file)
        filelist=""
        for file in files:
            filecreatedate = datetime.fromtimestamp(os.path.getctime(path_to_file + "\\" + file))
            fileinfo="Creation date of {}: {}".format(file, filecreatedate)
            print("Creation date of {}: {}".format(file, filecreatedate))
            filelist=filelist + fileinfo + "\n"
        return filelist
        # return os.path.getctime(path_to_file)
    else:
        stat = os.stat(path_to_file)
        try:
            return stat.st_birthtime
        except AttributeError:

            return stat.st_mtime

def mailsection(filelist):
    smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
    smtpObj.ehlo()
    (250, b'mx.example.com at your service, [216.172.148.131]\nSIZE 35882577\
    n8BITMIME\nSTARTTLS\nENHANCEDSTATUSCODES\nCHUNKING')
    smtpObj.starttls()
    (220, b'2.0.0 Ready to start TLS')
    smtpObj.login('********', '*********')
    (235, b'2.7.0 Accepted')
    message="Subject:currentfiles\n" + filelist
    smtpObj.sendmail('********', '********', message)
    {}
    smtpObj.quit()
    (221, b'2.0.0 closing connection ko10sm23097611pbd.52 - gsmtp')

    mailsection(creation_date("C:\\Users\\asher.vast\\Desktop\\Py\\Projects\\Time Tracker\\ExampleDir"))

1 个答案:

答案 0 :(得分:0)

我的一位朋友在工作中帮助了我。我们最终使用“电子邮件消息类”,允许换行。

message = EmailMessage()
    message["Subject"] = "Current Files"
    message["From"] = "John Doe <*************@gmail.com>"
    message["To"] = "Recipient X <*************.com>"
    message.set_content(filelist, subtype="html")
    ####message="Subject:currentfiles\r\n" + filelist
    smtpObj.send_message(message)