-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
186 lines (115 loc) · 3.51 KB
/
Copy pathmain.py
File metadata and controls
186 lines (115 loc) · 3.51 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# ==========================================
# CodeAlpha Project 03 - Email Extractor
# Developed by: Dua Sajjad
# ==========================================
import re
import os
INPUT_FILE = "input.txt"
OUTPUT_FILE = "emails.txt"
emails = []
# ---------------- CLEAR SCREEN ---------------- #
def clear():
os.system("cls" if os.name == "nt" else "clear")
# ---------------- PAUSE ---------------- #
def pause():
input("\nPress Enter to continue...")
# ---------------- MENU ---------------- #
def show_menu():
clear()
print("=" * 55)
print(" EMAIL EXTRACTOR TOOL")
print("=" * 55)
print("1. Read Input File")
print("2. Extract Emails")
print("3. View Extracted Emails")
print("4. Show Total Emails")
print("5. Save Emails")
print("6. Exit")
print("=" * 55)
# ---------------- READ FILE ---------------- #
def read_file():
if not os.path.exists(INPUT_FILE):
print(f"\n'{INPUT_FILE}' does not exist.")
return None
try:
with open(INPUT_FILE, "r", encoding="utf-8") as file:
data = file.read()
if not data.strip():
print("\nInput file is empty.")
return None
print("\nInput file loaded successfully!")
return data
except Exception as e:
print(f"\nError: {e}")
return None
# ---------------- EXTRACT EMAILS ---------------- #
def extract_emails():
global emails
text = read_file()
if text is None:
return
pattern = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
found = re.findall(pattern, text)
emails = sorted(list(set(found)))
if emails:
print(f"\n{len(emails)} email(s) extracted successfully!")
else:
print("\nNo email addresses found.")
# ---------------- VIEW EMAILS ---------------- #
def view_emails():
if not emails:
print("\nNo emails extracted yet.")
return
print("\n" + "=" * 55)
print(" EXTRACTED EMAIL ADDRESSES")
print("=" * 55)
for i, email in enumerate(emails, start=1):
print(f"{i}. {email}")
# ---------------- TOTAL EMAILS ---------------- #
def total_emails():
print("\n" + "=" * 55)
print(f"Total Emails Found : {len(emails)}")
print("=" * 55)
# ---------------- SAVE EMAILS ---------------- #
def save_emails():
if not emails:
print("\nNothing to save.")
return
try:
with open(OUTPUT_FILE, "w", encoding="utf-8") as file:
file.write("=" * 40 + "\n")
file.write("EMAIL EXTRACTION REPORT\n")
file.write("=" * 40 + "\n\n")
for email in emails:
file.write(email + "\n")
file.write("\n")
file.write(f"Total Emails : {len(emails)}")
print(f"\nEmails saved successfully to '{OUTPUT_FILE}'")
except Exception as e:
print(f"\nError: {e}")
# ---------------- MAIN PROGRAM ---------------- #
while True:
show_menu()
choice = input("Enter your choice (1-6): ")
if choice == "1":
read_file()
pause()
elif choice == "2":
extract_emails()
pause()
elif choice == "3":
view_emails()
pause()
elif choice == "4":
total_emails()
pause()
elif choice == "5":
save_emails()
pause()
elif choice == "6":
print("\nThank you for using Email Extractor!")
print("Have a Great Day!")
break
else:
print("\nInvalid choice!")
pause()