-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-email-from-text.py
50 lines (42 loc) · 1.07 KB
/
get-email-from-text.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
# this script will open a file with email addresses in it, then extract
# those address and write them to a new file e.g. extract-emails.txt
#replace users.csv with your filename+extension
import os
import re
# vars for filenames
filename = 'users.csv'
newfilename = 'extract-emails.txt'
# read file
if os.path.exists(filename):
data = open(filename,'r')
bulkemails = data.read()
else:
print "File not found."
raise SystemExit
# regex = whoEver@wHerever.xxx
r = re.compile(r'(\b[\w.]+@+[\w.]+.+[\w.]\b)')
results = r.findall(bulkemails)
emails = ""
for x in results:
emails += str(x)+"\n"
# function to write file
def writefile():
f = open(newfilename, 'w')
f.write(emails)
f.close()
print "File written."
# function to handle overwrite question
def overwrite_ok():
response = raw_input("Are you sure you want to overwrite "+str(newfilename)+"? Yes or No\n")
if response == "Yes":
writefile()
elif response == "No":
print "Aborted."
else:
print "Please enter Yes or No."
overwrite_ok()
# write/overwrite
if os.path.exists(newfilename):
overwrite_ok()
else:
writefile()