Sorting File Words Alphabetically

  • Share this:

Code introduction


This function reads the content of the file at the specified file path, splits all words, converts them to lowercase, and then sorts them alphabetically.


Technology Stack : File operation, string handling, sorting

Code Type : File processing function

Code Difficulty : Intermediate


                
                    
def sorted_words(file_path):
    with open(file_path, 'r') as file:
        words = file.read().split()
    sorted_words = sorted(words, key=lambda x: x.lower())
    return sorted_words