Remove repeated words using python

Question:

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

 import  codecs

 wordList = codecs.open('Arquivo.txt' , 'r')
 wordList2 = codecs.open('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:
             wordList.remove(y)

Error presented

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

Answer:

Instead of opening the files like this:

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

Try it like this:

wordList = codecs.open('Arquivo.txt' , 'r').readlines()
wordList2 = codecs.open('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