write a program that takes as an input a text file. Your program should print all the unique words in the file in alphabetical order.

write a program that takes as an input a text file. Your program should print all the unique words in the file in alphabetical order.


def unique_file(input_filename, output_filename):
    input_file = open(input_filename, 'r')
    file_contents = input_file.read()
    input_file.close()
    duplicates = []
    word_list = file_contents.split()
    file = open(output_filename, 'w')
    for word in word_list:
        if word not in duplicates:
            duplicates.append(word)
            file.write(str(word) + "\n")
    file.close()


unique_file('sample.txt','unique_output.txt')
for line in sorted(open('unique_output.txt')):
    print(line, end='')


Output

This
is
the
most
import
task.
you
have
performed
in
a
while.
python
rocks
yeah.

Print this post

Comments

Post a Comment

Popular posts from this blog

Global Bikes INC(GBI) Software Requirements Specifications (SRS)

Perform an in-depth evaluation of the following websites: Website 1: http://jbhifi.com.au Website 2: https://www.amazon.com