Python之篩選資料夾目錄下的檔案

2021-09-25 21:29:35 字數 3574 閱讀 9013

例如在測試過程中會生成多個測試結果,我們需要獲取當前最新的測試結果,然後通過郵件的方式傳送出去。

首先、要做的是進行篩選最新的檔案。

# -*- coding: utf-8 -*-

import smtplib

from email.mime.text import mimetext

from email.header import header

from email.mime.multipart import mimemultipart

# -----自動化執行結果路徑資訊-------------------

#測試結果存放的目錄,使用相對路徑

dir = '../test_report/report/html/'

# -----html報告:獲取路徑下最新的檔案---------

items = os.listdir(dir)

items.sort(reverse=false) # 公升序

html_report = items[len(items) - 1]

# -----html報告:從最新的檔案中擷取時間內容,倒數第6-22個字元----------

now_date = html_report[-21:-5]

# -----html報告:拼寫html報告的發布路徑---------

url_report = dir + html_report

# -----執行結果記錄:reportlist.txt--------

# -----讀取dd_inte***ce_errlog.txt檔案,計算檔案字元數量---------

try:

error_data = file_object_error.read()

finally:

file_object_error.close()

print(len(error_data))

# -----當檔案字元數量>100個,則存在異常資訊,將報告名稱寫入reportlist.txt檔案---------

# -----當檔案字元數量≤100個,則執行結果無異常,清空reportlist.txt檔案--------

if len(error_data) > 100:

fileobjectwrite = open('reportlist.txt', 'a')

fileobjectwrite.write(url_report+' \n')

fileobjectwrite.close()

else:

fileobjectwrite = open('reportlist.txt', 'w')

fileobjectwrite.write('')

fileobjectwrite.close()

# -----讀取檔案內容-------

fileobject = open('reportlist.txt', 'r')

# -----讀取檔案內容行數-------

lines = fileobject.readlines()

print(len(lines))

# -----內容行收尾去空格-------

for line in lines: # 依次讀取每行

line = line.strip() # 去掉每行頭尾空白

print("讀取的資料為: %s" %(line))

# -----列印內容行內容---------

print(lines)

然後,配置郵件項,然後把檔案新增到郵件的附件中

# -----郵件資訊-----------

smtpserver = 'smtp.qq.com'

smtpport = 465

sender_name = 'auto_reporter' # from_mail

sender = '[email protected]' # from_mail

sender_key = '110' # from_mail,授權碼

receivers = ['[email protected]'] # 接收郵件,可設定為你的qq郵箱或者其他郵箱

subject = html_report + '_檢查失敗'

body = subject + """

測試報告詳情參考:

""" + url_report

# 三個引數:第乙個為文字內容,第二個 plain 設定文字格式,第三個 utf-8 設定編碼

message = mimemultipart()

message['from'] = header(sender_name) # 傳送者,from_mail

message['to'] = header(','.join(receivers), 'utf-8') # 接收者, to_mail

message['subject'] = header(subject, 'utf-8')

attach1 = mimetext(open(dir+html_report, 'rb').read(), "base64", "utf-8")

# 這裡filename即附件的顯示名字,將會在郵件中顯示

attach1["content-disposition"] = "attrachment;filename=html_report.html"

# 關聯附件到郵件中

message.attach(attach1)

最後,判斷當有最新的測試結果時,執行傳送郵件,並附帶測試結果。

# -----mail通知發布機制:連續n次錯誤,傳送mail通知--------

# -----當slack通知發布成功後,清空reportlist.txt檔案--------

# -----當slack通知發布失敗後,列印傳送資訊失敗--------------

if len(lines) >= 0:

try:

smtpobj = smtplib.smtp_ssl(smtpserver, smtpport)

smtpobj.login(sender, sender_key)

smtpobj.sendmail(sender, receivers, message.as_string())

print('郵件傳送成功')

fileobject.close()

fileobjectwrite = open('reportlist.txt', 'w')

fileobjectwrite.write('')

print('迴圈次數檔案已清空')

fileobjectwrite.close()

except smtplib.smtpexception:

print('error: 無法傳送郵件')

fileobject.close()

finally:

smtpobj.quit()

else:

fileobject.close()

quit()

Apache Tomcat目錄下各個資料夾的作用

1.bin 存放各種不同平台開啟與關閉tomcat的指令碼檔案。2.lib 存tomcat與web應用的jar包。3.conf 存放tomcat的配置檔案。5.work tomcat把由各種jsp生成的servlet檔案存放的地方。6.logs tomcat存放日誌檔案的地方。7.temp tomc...

刪除目錄(資料夾)以及目錄下的檔案

刪除目錄 資料夾 以及目錄下的檔案 param spath 被刪除目錄的檔案路徑 return 目錄刪除成功返回true,否則返回false public static boolean deletedirectory string spath file dirfile new file spath ...

Python 之 建立目錄資料夾

python對檔案的操作還算是方便的,使用os模組即可實現檔案路徑操作,相關函式介紹如下。1 os.path.exists path 判斷乙個目錄是否存在 2 os.makedirs path 多層建立目錄 3 os.mkdir path 建立目錄 def mkdir path 引入模組 impor...