Skip to content

Instantly share code, notes, and snippets.

@FabianBartl
Last active February 28, 2025 14:28
Show Gist options
  • Save FabianBartl/7fefd07599246426a20d10ad2ef85c9d to your computer and use it in GitHub Desktop.
Save FabianBartl/7fefd07599246426a20d10ad2ef85c9d to your computer and use it in GitHub Desktop.
Extracts every unique email from multiple text files and copies the result into your clipboard. So (at least on windows) you can just drag-n-drop any amount of txt-files onto the python script in your file explorer, it opens a terminal, prints the emails and puts them into your clipboard.
"""
Extracts every unique email from multiple text files and copies the result into your clipboard.
So (at least on windows) you can just drag-n-drop any amount of txt-files onto the python script
in your file explorer, it opens a terminal, prints the emails and puts them into your clipboard.
"""
import sys
import os
import re
from pathlib import Path
copy_to_clipboard = False
try:
import pyperclip
copy_to_clipboard = True
except ModuleNotFoundError as err:
print("run 'pip install pyperclip' for full functionallity")
input_files = []
for arg in sys.argv[1:]:
path = Path(arg)
if path.exists() and path.is_file():
input_files.append(path)
# https://stackoverflow.com/a/201378/15774644
# https://emailregex.com/
email_pattern = r"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)"
unique_emails = set()
for path in input_files:
with open(path, "r", encoding="utf-8") as fobj:
content = fobj.read()
found_emails = re.findall(email_pattern, content)
unique_emails.update(set( map(str.lower, found_emails) ))
emails_str = "; ".join(sorted(unique_emails)) + ";\n"
if copy_to_clipboard:
pyperclip.copy(emails_str)
print(f"Copied {len(unique_emails)} emails to your clipboard:\n")
print(emails_str)
input("\npress ENTER to exit\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment