162k views
2 votes
Suppose that a text file contains an unspecified number of English words separated by blank spaces. Write a program that prompts the user to enter the file name, reads the words from the file, and displays the words in alphabetical order. For example, for a file that contains

"ask not what your country can do for you ask what you can do for your country"

your program should output

ask
can
country
do
for
not
what
you
your

User Savion
by
6.3k points

1 Answer

2 votes

In python 3.8:

f = open(input("Enter the name of your file (with the extension): "), "r")

l = sorted(set(f.read().split()))

for x in l:

print(x)

If your text file was named TextDocument, you would have to enter: TextDocument.txt

User KedarX
by
6.6k points