|
1 | 1 | import csv |
2 | 2 | import smtplib, ssl |
3 | 3 |
|
4 | | -message="""Hi {fname}, Wish you a very Happy birthday. |
5 | | -Hope you had a great time.""" |
| 4 | +message = """Hi {fname}, |
| 5 | +I wish you a very Happy Birthday. |
| 6 | +I hope you had a great day.""" |
6 | 7 |
|
7 | | -sender_address="forpythonbirthdayproject@gmail.com" |
8 | | -sender_password="qwerty@12345" |
| 8 | +sender_address = "you@example.com" |
| 9 | +sender_password = "examplePassword" |
9 | 10 |
|
10 | 11 | context = ssl.create_default_context() |
11 | 12 | with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: |
12 | | - server.login(sender_address,sender_password) |
| 13 | + server.login(sender_address, sender_password) |
13 | 14 |
|
14 | | - with open(r"C:\Users\sriva\OneDrive\Desktop\VS Code\Python\birthday.csv") as file: |
15 | | - reader=csv.reader(file) |
| 15 | + with open(r"birthday.csv") as birthdays: |
| 16 | + reader = csv.reader(birthdays) |
16 | 17 | next(reader) |
17 | | - for fname,lname,email,dob in reader: |
| 18 | + for fname, lname, email, dob in reader: |
18 | 19 | server.sendmail( |
19 | 20 | sender_address, |
20 | 21 | email, |
21 | 22 | message.format(fname=fname), |
22 | 23 | ) |
23 | 24 |
|
24 | | -add=input( |
25 | | - """Do you wish to add or remove names to the csv file? |
26 | | - if you want add names type add |
27 | | - otherwise if you want remove names type remove |
28 | | - if you wish to exit, type exit |
| 25 | +choice = input( |
| 26 | + """Do you wish to add or remove names from the csv file? |
| 27 | + if you would like to add names type add |
| 28 | + otherwise if you would like to remove names type remove |
| 29 | + When you are finished, type exit |
29 | 30 | """ |
30 | 31 | ) |
31 | | - |
32 | | -if add=="add": |
33 | | - new_data=input("Enter data as first name,lastname,email,date of birth: ") |
34 | | - new_data=new_data.split(",") |
35 | | - with open(r"C:\Users\sriva\OneDrive\Desktop\VS Code\Python\birthday.csv",'r+') as file: |
36 | | - writer_object=csv.writer(file) |
| 32 | + |
| 33 | +if choice == "add": |
| 34 | + new_data = input("Enter data as first name,lastname,email,date of birth: ") |
| 35 | + new_data = new_data.split(",") |
| 36 | + with open(r"birthday.csv", "r ") as file: |
| 37 | + writer_object = csv.writer(file) |
37 | 38 | next(file) |
38 | 39 | writer_object.writerow(new_data) |
39 | | -elif add=="remove": |
40 | | - lines=[] |
41 | | - removal=input("Enter the first name of the person to be removed: ") |
42 | | - with open(r"C:\Users\sriva\OneDrive\Desktop\VS Code\Python\birthday.csv",'r') as file: |
43 | | - reader=csv.reader(file) |
| 40 | +elif choice == "remove": |
| 41 | + lines = [] |
| 42 | + removal = input("Enter the first name of the person to be removed: ") |
| 43 | + with open(r"birthday.csv", "r") as file: |
| 44 | + reader = csv.reader(file) |
| 45 | + |
44 | 46 | for row in reader: |
45 | 47 | lines.append(row) |
| 48 | + |
46 | 49 | for fields in row: |
47 | | - if fields==removal: |
| 50 | + if fields == removal: |
48 | 51 | lines.remove(row) |
49 | | - with open(r"C:\Users\sriva\OneDrive\Desktop\VS Code\Python\birthday.csv",'w',newline="") as file: |
50 | | - writer=csv.writer(file) |
51 | | - writer.writerows(lines) |
52 | | - |
53 | | - |
54 | 52 |
|
| 53 | + with open(r"birthday.csv", "w", newline="") as file: |
| 54 | + writer = csv.writer(file) |
| 55 | + writer.writerows(lines) |
55 | 56 |
|
0 commit comments