This Python script is designed to process a file, such as a SQL file, and extract all unique email addresses found within it. It uses a sophisticated regular expression to identify email addresses and adds each unique email to a set to ensure there are no duplicates.
The code consists of a single function called find_unique_emails
that takes a file path as an argument. Here's a step-by-step breakdown of how the function works:
- The function defines a regular expression pattern
email_pattern
that matches email addresses in the formatname@domain.tld
. - It initialises a set called
unique_emails
to store the unique email addresses. - The function attempts to open the file specified by the
file_path
argument in read mode. - If the file is successfully opened, the function prints a message indicating that it is searching for email addresses.
- It then reads the file line by line and uses the
re.findall
function to extract all email addresses found in each line. - For each email address found, the function checks if it is already in the
unique_emails
set. If not, it prints the email address and adds it to the set. - After processing the entire file, the function prints the total number of unique email addresses found.
- If the file is not found, the function prints an error message indicating that the file was not found.
- If any other exception occurs, the function prints a generic error message.
To use the script, follow these steps:
- Ensure you have Python installed on your system.
- Save the Python script in a directory that contains the file you want to process.
- Open a terminal or command prompt and navigate to the directory where the script is located.
- Modify the
file_path
variable in the script to point to the file you want to process (e.g.,file_path = 'database.sql'
). - Run the script by typing
python UniqueEmailExtractorFromSQL.py
in the terminal. - The script will process the file and print each unique email address found, followed by the total count of unique email addresses.
Searching for email addresses in the file...
Found email: example1@example.com
Found email: example2@example.org
Found email: example3@example.net
Total unique email addresses found: 3
We welcome contributions from the community! If you'd like to contribute, please fork the repository, create a new branch, and submit a pull request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/AmazingFeature
). - Commit your changes (
git commit -m 'Add some AmazingFeature'
). - Push to the branch (
git push origin feature/AmazingFeature
). - Open a Pull Request.
If this script has been helpful for you, consider making a donation to support our work:
USDT (TRC-20): TGpiWetnYK2VQpxNGPR27D9vfM6Mei5vNA
Your donations help us continue developing useful and innovative tools.
Leading the Digital Revolution as the Pioneering 100% Artificial Intelligence Team.
- Copyright (c)
- Author: David C Cavalcante
- LinkedIn: linkedin.com/in/hellodav
- X: @Takk8IS
- Medium: takk8is.medium.com
- Website: takk.ag