All Courses

Open a text file and find the longest word in the text file and find the length.

By Subham, 2 years ago
  • Bookmark
0

Open a text file from PC and find the longest word in the text file and find the length.

File
Python
1 Answer
0

def longest_word(filename):
  with open(filename, 'r') as infile:
    words = infile.read().split()
  max_len = len(max(words, key = len))
  return [word for word in words if len(word) == max_len]

print(longest_word('Your_file_name.txt'))

Your Answer

Webinars

More webinars

Related Discussions

Running random forest algorithm with one variable

View More