Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A python script for extracting email addresses from text files.You can pass it multiple files. It prints the email addresses to stdout, one address per line.For ease of use, remove the .py extension and place it in your $PATH (e.g. /usr/local/bin/) to run it like a built-in command.

The program below can take one or more plain text files as input. It works with python2 and python3.

Let's say we have two files that may contain email addresses:

  1. file_a.txt
foo bar
ok sup,wyd
hello world!

  1. file_b.html
    <li><span class=pl-c>Dennis Ideler &lt;;</span></li>
    <li><span class=pl-c>Jane Doe &lt;;</span></li>

To extract the email addresses, download the Python program and execute it on the command line with our files as input.

$ python file_a.txt file_b.html

Voila, it prints all found email addresses. Let's also remove the duplicates and sort the email addresses alphabetically.

$ python file_a.txt file_b.html | sort | uniq

Looks good! Now let's save the results to a file.

$ python file_a.txt file_b.html | sort | uniq > emails.txt

P.S. The above commands for sorting and deduplicating are specific to shells on a UNIX-based machine (e.g. Linux or Mac). If you're using Windows, you can use PowerShell. For example

python file_a.txt file_b.html | sort -unique
#!/usr/bin/env python
# Extracts email addresses from one or more plain text files.
# Notes:
# - Does not save to file (pipe the output to a file if you want it saved).
# - Does not check for duplicates (which can easily be done in the terminal).
# (c) 2013 Dennis Ideler <>
from optparse import OptionParser
import os.path
import re
regex = re.compile(("([a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`"
def file_to_str(filename):
"""Returns the contents of filename as a string."""
with open(filename) as f:
return # Case is lowered to prevent regex mismatches.
def get_emails(s):
"""Returns an iterator of matched emails found in string s."""
# Removing lines that start with '//' because the regular expression
# mistakenly matches patterns like '' as '//'.
return (email[0] for email in re.findall(regex, s) if not email[0].startswith('//'))
if __name__ == '__main__':
parser = OptionParser(usage="Usage: python %prog [FILE]...")
# No options added yet. Add them here if you ever need them.
options, args = parser.parse_args()
if not args:
for arg in args:
if os.path.isfile(arg):
for email in get_emails(file_to_str(arg)):
print('"{}" is not a file.'.format(arg))
Copy link

bryanseah234 commented Sep 2, 2020

I keep getting this error though... anyone knows why?

File "", line 29, in file_to_str return # Case is lowered to prevent regex mismatches. File "C:\Users\bryan\AppData\Local\Programs\Python\Python38-32\lib\encodings\", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 164972: character maps to

Copy link

dideler commented Sep 4, 2020

@bryanseah234 the file you're reading has an unexpected encoding. Try setting the appropriate encoding for the file you're reading.

E.g. Change open(filename) to open(file, encoding="utf8") or open(file, encoding="latin-1").

Copy link

paseman commented Jan 4, 2021


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment