python實現(xiàn)定時壓縮指定文件夾發(fā)送郵件
工作中每天需要收集部門內(nèi)的FR文件,發(fā)送給外部部門的同事幫忙上傳,這么發(fā)了有大半年,昨天亮光一閃,為什么不做成自動化呢,于是用python實現(xiàn)了整個流程,今天體驗了一下真是美滋滋。
代碼如下
首先導入需要的包
import win32com.client as win32 import datetime import os import zipfile
定義三個函數(shù),都是網(wǎng)上抄別的同學作業(yè)來的(侵刪)
郵箱用的是outlook
#壓縮文件夾函數(shù) def zip_ya(startdir,file_news): file_news = startdir +'.rar' # 壓縮后文件夾的名字 z = zipfile.ZipFile(file_news,'w',zipfile.ZIP_DEFLATED) #參數(shù)一:文件夾名 for dirpath, dirnames, filenames in os.walk(startdir): fpath = dirpath.replace(startdir,'') #這一句很重要,不replace的話,就從根目錄開始復制 fpath = fpath and fpath + os.sep or ''#這句話理解我也點郁悶,實現(xiàn)當前文件夾以及包含的所有文件的壓縮 for filename in filenames: z.write(os.path.join(dirpath, filename),fpath+filename) print ('壓縮成功') z.close()
#創(chuàng)建文件夾函數(shù) def mkdir(path): folder = os.path.exists(path) if not folder: os.makedirs(path) print "創(chuàng)建文件夾成功" else: print "文件夾已存在"
#發(fā)送郵件函數(shù) def sendmail(path): sub = '上傳FR文件申請' body = '@xx,\r請幫忙上傳FR文件,謝謝!' outlook = win32.Dispatch('outlook.application') receiver = ['xxx@xx.com'] ccreceiver = ['xxx@xx.com;xxx@xx.com;xxx@xx.com;xxx@xx.com'] mail = outlook.CreateItem(0) mail.To = receiver[0] mail.Cc = ccreceiver[0] mail.Subject = sub.decode('utf-8') mail.Body = body.decode('utf-8') mail.Attachments.Add(path) mail.Send()
文件夾名稱為日期,每天腳本運行時,會新建一個明天的文件夾,并把昨天的壓縮文件刪除,所以先定義幾個日期參數(shù)。
這里碰到一個坑,文件路徑含中文時,用這個函數(shù)os.path.exists()測試都是False,即沒有被識別到,用unicode(todaypath,'utf-8')轉為unicode后問題解決。
#獲取今天明天昨天的日期 today = datetime.date.today().strftime("%Y%m%d") tomorrow = (datetime.date.today()+ datetime.timedelta(days=1)).strftime("%Y%m%d") yesterday = (datetime.date.today()+ datetime.timedelta(days=-1)).strftime("%Y%m%d") #定義文件路徑 path='//tcent.cn/dfs/26.xx事業(yè)部/10.xx市場營銷中心/04.xxx部/02.xxx組/FR文件上傳/' todaypath=path + today todayfile = path + today + '.rar' tomorrowpath=path + tomorrow utodaypath=unicode(todaypath,'utf-8') utodayfile=unicode(todayfile,'utf-8') utomorrowpath=unicode(tomorrowpath,'utf-8') #定義昨天的壓縮文件 yesterdayfile=path + yesterday + '.rar' uyesterdayfile=unicode(yesterdayfile,'utf-8') #計算今天文件夾下的文件個數(shù) filenum = 0 for filename in os.listdir(utodaypath): filenum += 1 #創(chuàng)建明天的文件夾 mkdir(utomorrowpath) #刪除昨天的壓縮文件 if os.path.exists(uyesterdayfile): # 如果文件存在 os.remove(uyesterdayfile) else: print('no such file:%s'%uyesterdayfile)
在思考如何讓腳本每天自動運行時,決定采用windows定時任務配置(因為沒看懂python定時器..)但是windows只能設置為每天運行,實際上周末、節(jié)假日是不需要發(fā)送郵件的,而節(jié)假日補班時需要運行任務,可以在代碼端進行控制。
if條件那段就是先判斷是否是空文件夾,如果沒有文件就不用發(fā)了,如果有文件,再判斷今天的日期,決定要不要發(fā)郵件。
#獲取今天是周幾 weekoftoday=datetime.date.today().weekday() #節(jié)假日列表 holiday=['20180924','20181001','20181002','20181003','20181004','20181005'] #補班列表 workday=['20180924','20180925'] #是否是周末 isweekend=(weekoftoday == 5 or weekoftoday == 6) #是否是小長假 isholiday=today in holiday #是否不要補班 isworkday=today not in workday #文件夾是否為空 isnullfile=(filenum==0) #判斷是否要壓縮文件并發(fā)送郵件 #周末、工作日放假的節(jié)假日、文件夾為空時不執(zhí)行 #補班的周末例外 if isnullfile: pass else: if ((isweekend or isholiday) and isworkday ): pass else: #壓縮今天的文件夾 zip_ya(utodaypath,today) #發(fā)送郵件 sendmail(utodayfile)
最后把這個python存成bat文件,去windows定時任務里配置即可。
@echo off cd D:\myprograms\sendmail start python sendmail.py
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python中Dataframe數(shù)據(jù)排序方法(含實例講解)
在進行數(shù)據(jù)分析操作時,經(jīng)常需要對數(shù)據(jù)按照某行某列排序,或者按照多行多列排序,以及按照索引值排序等等,下面這篇文章主要給大家介紹了關于Python中Dataframe數(shù)據(jù)排序方法的相關資料,需要的朋友可以參考下2023-02-02windows10下python3.5 pip3安裝圖文教程
這篇文章主要為大家詳細介紹了windows10下python3.5 pip3安裝圖文教程,注意區(qū)分python 2.x和python 3.x的相關命令,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04詳解python如何在django中為用戶模型添加自定義權限
這篇文章主要介紹了python如何在django中為用戶模型添加自定義權限,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10