Created
April 2, 2016 08:06
-
-
Save Lvl4Sword/23ebfedcd1fd282c1c71de5fb00b0d77 to your computer and use it in GitHub Desktop.
E-mailing CSV + Rename + Archive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import os | |
import shutil | |
import sys | |
from envelopes import Envelope | |
def mail_send(files_to_send): | |
right_now = datetime.datetime.now() | |
envelope = Envelope( | |
from_addr = ('[email protected]', 'FROM_NAME'), | |
to_addr = ('[email protected]', 'TO_NAME'), | |
subject = 'THIS_IS_A_SUBJECT, | |
text_body = "" | |
) | |
full_files_to_send = [] | |
for each in files_to_send: | |
full_files_to_send.append(archive_path + each) | |
for each in full_files_to_send: | |
envelope.add_attachment(each) | |
envelope.send('smtp.googlemail.com', login='[email protected]', | |
password='THIS_IS_A_PASSWORD', tls=True) | |
source_path = "/home/user/SendFolder/" | |
archive_path = "/home/user/SendFolder/Archive/" | |
log_path = "/home/user/SendFolder/Logs/" | |
#create archive folder if not existent | |
if not os.path.isdir(archive_path): | |
) | |
print("archive folder was not found, creating folder...") | |
os.mkdir(archive_path) | |
# list to store .csv files | |
file_list = [] | |
# list to store files to be e-mailed | |
files_to_send = [] | |
for each in [x for x in os.listdir(source_path) if os.path.isfile(os.path.join(source_path, x))]: | |
if each.endswith('csv'): | |
file_list.append(each) | |
#check if csv files are found in file_list | |
if file_list != []: | |
for each in file_list: | |
# check if files already contain the timestamp of: "%Y%m%d_" (20160330) | |
try: | |
date_check = each.split('_')[0][:8] | |
year_ = int(date_check[0:4]) | |
month_ = int(date_check[4:6]) | |
day_ = int(date_check[6:8]) | |
datetime.date(year_, month_, day_) | |
# no timestamp | |
except ValueError as e: | |
value_error_message = str(e) | |
if value_error_message in ['year is out of range', 'month must be in 1..12', 'day is out of range for month']: | |
print('We have a timestamp, just not a valid one.') | |
print('Stripping invalid date, and placing a valid one.') | |
rename_file = each.split('_')[0][8:] | |
print("Renaming {0}".format(each)) | |
change_name = datetime.datetime.now().strftime("%Y%m%d_") + each | |
os.rename(source_path + each, source_path + change_name) | |
shutil.move(source_path + change_name, archive_path + change_name) | |
files_to_send.append(change_name) | |
# timestamp found | |
else: | |
# list of archive folder files | |
archive_files = [x for x in os.listdir(archive_path) if os.path.isfile(os.path.join(archive_path, x))] | |
# if file is already in the archive, pass | |
if each in archive_files: | |
pass | |
# it's not in the archive, send it there | |
else: | |
print("Renaming {0}".format(each)) | |
change_name = datetime.datetime.now().strftime("%Y%m%d_") + each | |
os.rename(source_path + each, source_path + change_name) | |
shutil.move(source_path + change_name, archive_path + change_name) | |
files_to_send.append(change_name) | |
mail_send(files_to_send) | |
# csv files don't exist in the source_dir | |
else: | |
print("No csv files found, exiting...") | |
sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment