PYTHON2
PYTHON2
1. Write a program that searches a directory and all of its subdirectories, recursively, and returns a
list of complete paths for all files with a given suffix.
file_suffix = input("Enter the file suffix to search for (e.g., .txt): ").strip()
if found_files:
print("Found files:")
1|
print(file_path)
else:
print(f"No files found with the suffix '{file_suffix}' in the Downloads directory.")
OUTPUT : -
2. Write python code to extract From: and To: Email Addresses from the given text file using regular
expressions. https://www.py4e.com/code3/mbox.txt.
import re
# Sample mbox text data (simulated format similar to mbox.txt)
sample_data = """
From: [email protected]
To: [email protected]
Subject: [sakai] svn commit: r39772 -
content/branches/sakai_2-5-x/contentimpl/impl/src/java/org/sakaiproject/content/impl
From: [email protected]
Python Assignment -1
2|Page BYHarithCM
Subject: [sakai] svn commit: r39771 - in sakai/tags/sakai-2.5.0/content: . branches/sakai_2-5-
0/content-impl/impl/src/java/org/sakaiproject/content/impl
From: [email protected]
To: [email protected]
Subject: [sakai] svn commit: r39770 - in sakai/trunk: .
contenttool/tool/src/java/org/sakaiproject/tool/content/client
...
"""
# Function to extract email addresses from text using regex
def extract_emails(text):
# Regular expression pattern to match email addresses email_pattern
= r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
# Search for From: and To: lines and extract email
addresses from_emails = re.findall(r'From: (\S+@\S+)',
text) to_emails = re.findall(r'To: (\S+@\S+)', text) return
from_emails, to_emails try:
# Extract From: and To: email addresses using the function
from_emails, to_emails = extract_emails(sample_data)
# Print the results
print("From:") for email
in from_emails:
print(email)
print("\nTo:") for
email in to_emails:
print(email)
except Exception as e:
print(f"Error: {e}")
3|
Page BYHarithCM
Python Assignment -1
OUTPUT : -
3. Consider the sentence “From [email protected] Fri Jan 4 14:50:18 2008”, Write python code to
extract email address and time of the day from the given sentence
import re def
extract_email_and_time(sentence):
# Regular expression pattern to match email address and time of the day
pattern = r'From (\S+@\S+) (\w{3} \w{3} \d{1,2} \d{2}:\d{2}:\d{2} \d{4})'
# Using re.search to find the pattern in the
sentence match = re.search(pattern, sentence) if
match:
email = match.group(1) # Extract email address
time_of_day = match.group(2) # Extract time of the day
return email, time_of_day
4|Page BYHarithCM
Python Assignment -1
else:
return None, None # Return None if no match found
OUTPUT : -
5|Page BYHarithCM
Python Assignment -1
4. Write a program to read, display and count number of sentences of the given
file import re def count_sentences(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read() print("File Content:")
print(content) sentences = re.split(r'[.!?]+',
content) num_sentences = len(sentences)
sentences = [s.strip() for s in sentences if s.strip()]
print(f"\nNumber of Sentences: {num_sentences}")
return num_sentences except FileNotFoundError:
print(f"Error: File '{file_path}' not
found.") return 0 except Exception as e:
print(f"Error: {e}") return 0
if __name__ == "__main__":
file_path = input("Enter the path of the file to read: ").strip()
count_sentences(file_path)
OUTPUT : -
5. Write a program that gets the current date and prints the day of the week.
6|Page BYHarithCM
Python Assignment -1
OUTPUT:-
7|Page BYHarithCM
Python Assignment -1
6. Write a function called print_time that takes two Time objects and prints total time it in the form
hour:minute:second.
if __name__ ==
"__main__": # Create two
Time objects t1 = Time(2,
30, 45) t2 = Time(1, 15,
20) # Print each Time
object print("Time 1:", t1)
print("Time 2:", t2)
# Calculate and print total time
print_time(t1, t2)
8|
Python Assignment -1
Page BYHarithCM
OUTPUT : -
7.Write a program that takes a birthday as input and prints the user’s age and the number of days,
hours, minutes and seconds until their next birthday
Page BYHarithCM
Python Assignment -1
OUTPUT : -
10 | P a g e BYHarithCM