python extract emails from text

#!/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))

Hot pastes

free anonymous http proxies
     21750 | Never
free anonymous socks5 proxies
     9810 | Never
maze traversal
     2344 | Never
windows license keys
     1946 | 2022-10-07
hello, world
     1901 | Never
Deep Copy vs Shallow Copy in C#
     1693 | Never
python extract emails from text
     1691 | Never
difference between iter and into_iter in rust
     1456 | Never
bitwise operations
     1285 | Never
flips a line of text so that it appears to be upside-down and mirrored
     1208 | Never