您好,登錄后才能下訂單哦!
工作中每天需要收集部門內(nèi)的FR文件,發(fā)送給外部部門的同事幫忙上傳,這么發(fā)了有大半年,昨天亮光一閃,為什么不做成自動(dòng)化呢,于是用python實(shí)現(xiàn)了整個(gè)流程,今天體驗(yàn)了一下真是美滋滋。
代碼如下
首先導(dǎo)入需要的包
import win32com.client as win32 import datetime import os import zipfile
定義三個(gè)函數(shù),都是網(wǎng)上抄別的同學(xué)作業(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的話,就從根目錄開始復(fù)制 fpath = fpath and fpath + os.sep or ''#這句話理解我也點(diǎn)郁悶,實(shí)現(xiàn)當(dāng)前文件夾以及包含的所有文件的壓縮 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()
文件夾名稱為日期,每天腳本運(yùn)行時(shí),會(huì)新建一個(gè)明天的文件夾,并把昨天的壓縮文件刪除,所以先定義幾個(gè)日期參數(shù)。
這里碰到一個(gè)坑,文件路徑含中文時(shí),用這個(gè)函數(shù)os.path.exists()測試都是False,即沒有被識(shí)別到,用unicode(todaypath,'utf-8')轉(zhuǎn)為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') #計(jì)算今天文件夾下的文件個(gè)數(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)
在思考如何讓腳本每天自動(dòng)運(yùn)行時(shí),決定采用windows定時(shí)任務(wù)配置(因?yàn)闆]看懂python定時(shí)器..)但是windows只能設(shè)置為每天運(yùn)行,實(shí)際上周末、節(jié)假日是不需要發(fā)送郵件的,而節(jié)假日補(bǔ)班時(shí)需要運(yùn)行任務(wù),可以在代碼端進(jìn)行控制。
if條件那段就是先判斷是否是空文件夾,如果沒有文件就不用發(fā)了,如果有文件,再判斷今天的日期,決定要不要發(fā)郵件。
#獲取今天是周幾 weekoftoday=datetime.date.today().weekday() #節(jié)假日列表 holiday=['20180924','20181001','20181002','20181003','20181004','20181005'] #補(bǔ)班列表 workday=['20180924','20180925'] #是否是周末 isweekend=(weekoftoday == 5 or weekoftoday == 6) #是否是小長假 isholiday=today in holiday #是否不要補(bǔ)班 isworkday=today not in workday #文件夾是否為空 isnullfile=(filenum==0) #判斷是否要壓縮文件并發(fā)送郵件 #周末、工作日放假的節(jié)假日、文件夾為空時(shí)不執(zhí)行 #補(bǔ)班的周末例外 if isnullfile: pass else: if ((isweekend or isholiday) and isworkday ): pass else: #壓縮今天的文件夾 zip_ya(utodaypath,today) #發(fā)送郵件 sendmail(utodayfile)
最后把這個(gè)python存成bat文件,去windows定時(shí)任務(wù)里配置即可。
@echo off cd D:\myprograms\sendmail start python sendmail.py
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。