def read_poem(file_name):
try:
with open(file_name, ‘r’) as file:
for line in file:
print(line.strip())
except FileNotFoundError:
print(f"The file ‘{file_name}’ does not exist.")
for line in file:
if not line.strip().lower().startswith('t'):
count += 1
file.close()
return count
except FileNotFoundError:
return -1
except Exception as e:
print(f"An error occurred: {e}")
return -2
filename = “story.txt”
result = count_lines_not_starting_with_T(filename)
if result == -1:
print(f"Error: File ‘{filename}’ not found.“)
elif result == -2:
print(“An error occurred while processing the file.”)
else:
print(f"Number of lines not starting with ‘T’: {result}”)
def countLinesNotStartingWithT(filename):
try:
# Open the file for reading
with open(filename, 'r') as file:
line_count = 0 # Initialize the line count
# Loop through each line in the file
for line in file:
line = line.strip() # Remove leading and trailing whitespace
print(line) # Display the line
# Check if the line does not start with 'T' or 't'
if not line.startswith(('T', 't')):
line_count += 1
# Print the count of lines not starting with 'T'
print(f"Number of lines not starting with 'T': {line_count}")
except FileNotFoundError:
print(f"File '{filename}' not found.")
# Call the function with the file name
countLinesNotStartingWithT("poem.txt")