write a program that inputs a text file. the program should print all of the unique words in the file in alphabetical order

In this article you will learn a Python program that asks the user to enter a text file.  then using the Python logic the program has to print the unique words present in the text file in alphabetical order A-Z and then a-z

Let's try to solve this Python program.

 

 

write a program that inputs a text file. the program should print all of the unique words in the file in alphabetical order


write a program that inputs a text file. the program should print all of the unique words in the file in alphabetical order

Take a string  variable named as file name and ask a user to enter the file name  using the input function call.


filename = input("Enter The Filename : ")




Using that try block take care file description variable and try to open the file in the read mode.

try:
   fd = open(filename, "r")




Use the  except block to catch the file not found exception,  and use the print statement to print the negative message and exit the program.

except FileNotFoundError:
   print("The file you entered does not exists")



If the file is found in the try block and it is in open State.  take a variable to store the words present in the string and initialize it  by reading all the words present in the file. Another is the list variable to store the unique words present in the file  initialized to an empty list.

Using the for loop, iterate all the words present in the file and check if count is equal to 1,  if the count is one up and the word to unique words.

Take another variable to store the sorted words and call the function to sort the words in ascending order,  using the print function call to print the sorted unique words.

else:
   words = fd.read().split()
   unique_words = []
   for word in words:
      if words.count(word) == 1:
          unique_words.append(word)
 
   sorted_unique_words = sorted(unique_words)
   print(sorted_unique_words)






Complete Python program
============================

filename = input("Enter The Filename : ")
try:
   fd = open(filename, "r")
except FileNotFoundError:
   print("The file you entered does not exists")
else:
   words = fd.read().split()
   unique_words = []
   for word in words:
      if words.count(word) == 1:
          unique_words.append(word)

   sorted_unique_words = sorted(unique_words)
   print(sorted_unique_words)





Content of Input File sample.txt
==========================
Write A Python Program To Demonstrate Break And Continue Statement
Write A Python Program To Print The Seventh Table And Store In File


Output
================
Enter The Filename : next.txt
['Break', 'Continue', 'Demonstrate', 'File', 'In', 'Print', 'Seventh', 'Statement', 'Store', 'Table', 'The']




Input File test.txt does not exist

Output 2
==================
Enter The Filename : test.txt
The file you entered does not exists




Conclusion
=================
The above Python program accepts the file name as input to it and opens the file in read mode. Gets the unique words present in the file by checking the number of times the word has occurred.

Once we get unique words present in the file, short the unique words in ascending order and print the unique sorted words as output.

Comment it down below if you have any query regarding the above Python program.


Post a Comment

0 Comments