# python获取邮件附件名称并标明日期(v20211109)
import email.header
import pathlib
import calendar
print("输入年份如:2021")
folder="d:\\"+input()
# print(folder)
txt_data = ""
p = pathlib.Path(folder).rglob("*")
for i in p:
fp = open(i, "r")
msg = email.message_from_file(fp)
# 以下提取邮件日期并格式化
date = msg.get("Date")
y = str(date[11:16])
y = y.replace(" ", "") # 清除空格
m = date[7:11]
m = m.replace(" ", "")
m = list(calendar.month_abbr).index(m) # 英文月份转数字
m = str(m)
if len(m) == 1:
m = "0" + m
d = str(date[4:7])
d = d.replace(" ", "")
if len(d) == 1:
d = "0" + d
ymd = y + m + d # 定义年月日格式
# 以下解析附件名称
for part in msg.walk():
filename = part.get_filename()
if filename:
s = filename
if 'UTF-8' in s or 'utf-8' in s:
for data, encoding in email.header.decode_header(s):
data = data.decode()
txt_data += ymd + data + "\n"
else:
txt_data += ymd + s + "\n"
fp = open("d:\\txt_data.txt", "w", encoding="utf-8")
fp.write(txt_data)
fp.close()
python获取邮件附件名称并标明日期(v20211109)
最新推荐文章于 2024-01-30 10:39:07 发布