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)
- Argument Checking: The script checks if the user provided a file name as an argument. If not, it displays a usage message and exits.
- File Name Retrieval: It retrieves the file name from the command line argument.
- File Reading: The script opens the file for reading and reads its contents into a variable called
text
. - 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. re.findall
: There.findall
function searches for all non-overlapping matches of the regular expression pattern in the text and returns them as a list.- 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 : Python