You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Exercise 8.5 Write a program to read through the mail box data and when you find line that starts with “From”, you will split the line into words using the split function. We are interested in who sent the message, which is the second word on the From line. You will parse the From line and print out the second word for each From line, then you will also count the number of From (not From:) lines and print out a count at the end.
while True :
fname = input('Enter a file name:')
try :
fhandle = open(fname)
except :
print('File not found')
continue
break
counter = 0
for line in fhandle :
line = line.rstrip()
words = line.split()
if len(words) > 0 and words[0] == 'From' :
counter = counter + 1
print(words[1])
print('There were', counter, 'lines in the file with From as the first word')