1def longestword(filename):
2 with open(filename,'r+') as f:
3 words = f.read().split()
4 max_len_word = max(words,key=len)
5 max_len = len(max(words,key=len))
6 print('maximum lenth word in file :',max_len_word)
7 print('lenth is : ',max_len)
8
9longestword('file1.txt')
10
11or
12
13def longest_word(filename):
14 with open(filename, 'r') as infile:
15 words = infile.read().split()
16 max_len = len(max(words, key=len))
17 return [word for word in words if len(word) == max_len]
18
19print(longest_word('file1.txt'))
20