一、遍歷報(bào)告文件夾內(nèi)容并按照時(shí)間倒敘排列供璧,取第一條文件(最新測(cè)試報(bào)告)
源碼
import os, glob, time
# 文件路徑
path= r'C:\Program Files\JetBrains\PyCharm\pythonProject\log'
# 返回指定的文件夾包含的文件或文件夾的名字的列表派诬。
# os.path.join(path1[, path2[, ...]])? 把目錄和文件名合成一個(gè)路徑
s= sorted(glob.glob(os.path.join(path, '*')),
? ? ? ? ? key=lambda x: time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getctime(x))),
? ? ? ? ? reverse=True)
# 輸出第一個(gè)文件
first= s[0]
print(first)
# 打開(kāi)文件
# os.startfile(first)
二蚊俺、發(fā)送郵箱,相關(guān)人員尿背,包含標(biāo)題,正文捶惜,附件(這邊使用的是Coremail論客郵箱)
import smtplib
from email.mime.textimport MIMEText
from email.headerimport Header
from email.mime.multipartimport MIMEMultipart
# 發(fā)送郵箱服務(wù)器
smtpsever= 'c1.icoremail.net'
# 發(fā)送郵箱用戶密碼
user= 'xxxxx@vsc.com'
password= 'passwd'
# 發(fā)送與接收郵箱田藐,可以多個(gè)逗號(hào)隔開(kāi)
sender= 'dingjj.nms@vsc.com'
receive= 'dingjj.nms@vsc.com'
# 發(fā)送郵件主題和內(nèi)容變量
subject= 'python selenium自動(dòng)化測(cè)試報(bào)告'
content= '<html><h1 style="color:green">郵件測(cè)試'
# 構(gòu)造附件內(nèi)容,由于GBK會(huì)報(bào)錯(cuò),所以轉(zhuǎn)換encoding="utf-8"格式
sendfile= open(first, 'r', encoding="utf-8").read()
att= MIMEText(str(sendfile), 'base64', 'utf-8')
att['Content-Type'] = 'application/octet-stream'
att['Content-Disposition'] = 'attachment;filename="log_20211221_183947.html"'
# 構(gòu)建發(fā)送與接收信息
msgRoot= MIMEMultipart()
# 調(diào)用主題
msgRoot['subject'] = subject
# 調(diào)用正文
msgRoot.attach(MIMEText(content, 'html', 'utf-8'))
# 調(diào)用郵箱賬號(hào)密碼
msgRoot['From'] = sender
# 調(diào)用接收人
msgRoot['To'] = ','.join(receive)
msgRoot.attach(att)
# SSL協(xié)議端口號(hào)要用465
smtp= smtplib.SMTP_SSL(smtpsever, 465)
smtp.helo(smtpsever)
smtp.ehlo(smtpsever)
smtp.login(user, password)
# msg為郵件正文吱七,msgRoot為附件
print('開(kāi)始發(fā)送郵件...')
smtp.sendmail(sender, receive, msgRoot.as_string())
smtp.quit()
print('郵件發(fā)送成功汽久!')