-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_file.py
125 lines (104 loc) · 4.08 KB
/
read_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
##################################################
##################################################
##################################################
##################################################
##################################################
################# Start of Read_File Script
#################
##################################################
##################################################
##################################################
##################################################
# Import pandas to read CSV to Dataframe
# Import sys for a system exit
# Import datetime for a record of file
# Import smtplib for sending the email
import pandas as pd
import sys
import datetime
import smtplib
###############################################
###############################################
########### Reads a CSV file and filters based
########### on specific email address ending.
########### Then it sends the result to
########### outgoing folder in the temp folder.
###############################################
###############################################
###############################################
###############################################
# Email Sending Function
def send_email(email, email_info):
# Start of email curation and connection
gmail_user = 'smtp_server' # server location for smtp
gmail_password = 'password'# authentication
# Formatting the Message
sent_from = gmail_user
to = email
subject = 'CSV Summary Data'
body = """
Job-run Date: %s
Job-run Time: %s
Total number of entries in the file: %i
Number of entries moved to c:/temp/outgoing: %i
""" % (email_info["date"], email_info["time"], email_info["start_entry"], email_info["end_entry"])
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), 'CSV Summary Data', body)
# Tries to econnect to the smtp server and send from the
# email made.
try:
smtp_server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
smtp_server.ehlo()
smtp_server.login(gmail_user, gmail_password)
smtp_server.sendmail(sent_from, to, email_text)
smtp_server.close()
print("Email Sent to ", email)
except Exception as ex:
print ("Something went wrong….",ex)
# Main function runs
if __name__ == '__main__':
email = sys.argv[1:]
# It tries to read csv and if it fails it
# exits gracefully
try:
df = pd.read_csv("temp/incoming/users.csv")
except FileNotFoundError:
print("There is no file to process")
sys.exit(0)
# Filters with the dataframe functions based on if
# it has @abc.edu
dff = df[df['emailaddress'].str.contains("@abc.edu")]
# Checks if dataframe is empty from filtering so
# exits gracefully
if dff.empty:
print('Nothing to move')
sys.exit(0)
# Gets current datetime and formats it for file
# record
x = datetime.datetime.now()
date_time = x.strftime("%Y%d%m%H%M%S")
email_info = {}
email_info["date"] = x.strftime("%m/%d/%Y")
email_info["time"] = x.strftime("%H:%M:%S")
email_info["start_entry"] = len(df)
email_info["end_entry"] = len(dff)
#Pushes out the csv to the outgoing folder
out_csv = "temp/outgoing/users"+ date_time +".csv"
dff.to_csv(out_csv, index=False)
print("CSV is sent to temp/outgoing folder")
send_email(email, email_info)
##################################################
##################################################
##################################################
##################################################
##################################################
################# End of Read_File Script
#################
##################################################
##################################################
##################################################
##################################################