This repository was archived by the owner on Dec 22, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +23
-19
lines changed Expand file tree Collapse file tree 1 file changed +23
-19
lines changed Original file line number Diff line number Diff line change 1
1
#!/usr/bin/env python3
2
2
import re
3
3
4
- print ("Enter the name of the input file: " )
5
- file = str (input ())
4
+ def main ():
5
+ print ("Enter the name of the input file: " )
6
+ file = str (input ())
7
+ return print_emails (get_emails (file ))
8
+ # Can add os.path functionality
6
9
7
- try :
8
- f = open (file ,"r" )
9
- except FileNotFoundError :
10
- print ("File does not exists" )
10
+ def get_emails (filename :str ):
11
+ """Function to return list of email matches found in filename passed"""
12
+ with open (filename ,'r' ) as file :
13
+ emails = []
14
+ for line in file :
15
+ #em = re.find('\S+@\S+\.\S+',line)
16
+ #print(em)
17
+ regex = re .match (r'\S+@\S+\.\S+' ,line ) # Creates a match object for a correct match
18
+ if regex : #if match exists
19
+ emails .append (regex .group (0 )) # extracts the text of the match
20
+
21
+ return emails
11
22
12
- email = {}
23
+ def print_emails (emails ):
24
+ """Simple printing function"""
25
+ for email in emails :
26
+ print (email )
13
27
14
- for i in f :
15
- em = re .findall ('\S+@\S+\.\S+' ,i )
16
- for j in em :
17
- email [j ]= email .get (j ,0 )+ 1
18
-
19
- f .close ()
20
-
21
- for i in email :
22
- if (email [i ]>= 2 ):
23
- print (i ,email [i ])
24
- else :
25
- print (i )
28
+ if __name__ == "__main__" :
29
+ main ()
You can’t perform that action at this time.
0 commit comments