Work every day need to collect FR files within the department, sent to colleagues in external departments to help upload, so sent for half a year, yesterday a flash of light, why not make it automated, so with python to realize the whole process, today experienced a really beautiful.
The code is as follows
First import the required packages
import as win32 import datetime import os import zipfile
Define three functions, all copied from other students' homework online (censored)
Email is outlook.
# Compress Folder Functions def zip_ya(startdir,file_news): file_news = startdir +'.rar' # Name of the compressed folder z = (file_news,'w',zipfile.ZIP_DEFLATED) # Parameter 1: Folder name for dirpath, dirnames, filenames in (startdir): fpath = (startdir,'') #This sentence is important, if you don't replace it, you'll start copying from the root directory. fpath = fpath and fpath + or ''#I'm also a bit depressed by this statement, which implements the compression of the current folder and all the files it contains. for filename in filenames: ((dirpath, filename),fpath+filename) print ('Compression successful') ()
# Create folder function def mkdir(path): folder = (path) if not folder: (path) print "Folder created successfully." else: print "Folder already exists."
#Send Mail Function def sendmail(path): sub = 'Upload FR file request' body = '@xx,\r please help upload the FR file, thanks!' outlook = ('') receiver = ['xxx@'] ccreceiver = ['xxx@;xxx@;xxx@;xxx@'] mail = (0) = receiver[0] = ccreceiver[0] = ('utf-8') = ('utf-8') (path) ()
The folder name is the date, and each day when the script runs, it creates a new folder for tomorrow and deletes yesterday's zip file, so define a few date parameters first.
Here encountered a pit, the file path containing Chinese, with this function () test are False, that is, not recognized, with unicode (todaypath, 'utf-8') to unicode after the problem is solved.
#Get today's tomorrow's yesterday's date today = ().strftime("%Y%m%d") tomorrow = (()+ (days=1)).strftime("%Y%m%d") yesterday = (()+ (days=-1)).strftime("%Y%m%d") #Define the file path path='///dfs/division/marketing-center/department/group/FR-file-upload/' 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') #defineyesterday's zip file yesterdayfile=path + yesterday + '.rar' uyesterdayfile=unicode(yesterdayfile,'utf-8') #Calculate the number of files in today's folder filenum = 0 for filename in (utodaypath): filenum += 1 #Creating tomorrow's folder mkdir(utomorrowpath) #Delete yesterday's zip file if (uyesterdayfile): # If the file exists (uyesterdayfile) else: print('no such file:%s'%uyesterdayfile)
When thinking about how to make the script run automatically every day, I decided to use windows timed task configuration (because I didn't read the python timer...) But windows can only be set to run every day, in fact, there is no need to send emails on weekends and holidays, and the task needs to run on holiday make-up work, which can be controlled on the code side.
The if condition bit is to first determine if it is an empty folder, if there are no files you don't have to send it, if there are files, then determine today's date and decide if you want to send the email or not.
# Get what day of the week it is weekoftoday=().weekday() # List of holidays holiday=['20180924','20181001','20181002','20181003','20181004','20181005'] #The list of remedial classes workday=['20180924','20180925'] #Is it the weekend isweekend=(weekoftoday == 5 or weekoftoday == 6) # Is it a mini-vacation isholiday=today in holiday #Whether or not to make up classes isworkday=today not in workday # whether the folder is empty or not isnullfile=(filenum==0) # Determine if you want to zip a file and send an email #Not implemented on weekends, weekdays off holidays, when folders are empty #Exception for make-up weekends if isnullfile: pass else: if ((isweekend or isholiday) and isworkday ): pass else: # Compress today's folder zip_ya(utodaypath,today) #Send Mail sendmail(utodayfile)
Finally, just save this python as a bat file and go to windows timed tasks to configure it.
@echo off cd D:\myprograms\sendmail start python
This is the whole content of this article.