Python Script: Extracting email addresses from a text file

python @ Freshers.in

In this article, we’ll guide you through the process of creating a Python script for this purpose, explain each part of the script, and provide a detailed example. Here’s a Python script that takes a file name as an argument and extracts all the email addresses from it:

import re
import sys

# Check if the user provided a file name as an argument.
if len(sys.argv) != 2:
    print("Usage: python extract_emails.py <file_name>")
    sys.exit(1)
# Get the file name from the command line argument.
file_name = sys.argv[1]

try:
    # Open the file for reading.
    with open(file_name, 'r') as file:
        text = file.read()
        # Use regular expressions to find email addresses.
        email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b'
        emails = re.findall(email_pattern, text)
        # Print the extracted email addresses.
        for email in emails:
            print(email)
except FileNotFoundError:
    print(f"Error: File '{file_name}' not found.")
    sys.exit(1)
  1. Argument Checking: The script checks if the user provided a file name as an argument. If not, it displays a usage message and exits.
  2. File Name Retrieval: It retrieves the file name from the command line argument.
  3. File Reading: The script opens the file for reading and reads its contents into a variable called text.
  4. Regular Expression for Email: It uses a regular expression (email_pattern) to find email addresses within the text. The regular expression pattern matches standard email formats.
  5. re.findall: The re.findall function searches for all non-overlapping matches of the regular expression pattern in the text and returns them as a list.
  6. Displaying Email Addresses: The script iterates through the list of email addresses and prints each one.

Let’s assume you have a text file named emails.txt containing email addresses. To extract the email addresses from this file, you can use the script as follows:

python extract_emails.py emails.txt
Refer more on python here :
Author: user