Remove repeated words using python


I have a text file with many repeated words. I need each word in the file to appear only once.

 import  codecs

 wordList ='Arquivo.txt' , 'r')
 wordList2 ='Arquivo2.txt', 'w')

 for x in range(len(wordList)) :
    for y in range(x + 1, len(wordList ) ):
        if wordList[x] == wordList[y]:
            wordList2.append(wordList[x] )
        for y in wordList2:

Error presented

     for x in range(len(wordList)):
 TypeError: object of type 'file' has no len()


Instead of opening the files like this:

wordList ='Arquivo.txt' , 'r')
wordList2 ='Arquivo2.txt', 'w')

Try it like this:

wordList ='Arquivo.txt' , 'r').readlines()
wordList2 ='Arquivo2.txt', 'w')

I recommend that you also take a look at the python coding style guide . Using CamelCase for variable names is not recommended in python.

Scroll to Top