Documentation
The PEP 8 style guide recommends adding docstrings for functions. For example:
def extract_unique_emails(filename):
"""
Extract unique email addresses from a text file.
The input is a path to a test file.
The function returns a list of email addresses.
"""
Simpler
These 2 lines:
unique_emails = sorted(set(emails))
return unique_emails
can be combined into 1 line:
return sorted(set(emails))
Similarly, these lines:
emails = extract_unique_emails("sample.txt")
for email in emails:
can be combined:
for email in extract_unique_emails("sample.txt"):
The regular expression:
email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
can be simplified using \d, \w and the IGNORECASE flag:
email_pattern = r'[a-z\dr'[\w._%+%+-]+@[a-z\d.-]+\.[a-z]{2,}'
emails = re.findall(email_pattern, content, re.IGNORECASE)