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

By Subham, 6 months 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
We're Online!

Chat now for any query