I have two text files. One of them is the whole text (text1) and the other is the number of unique words in text1. I need to calculate a monogram and then write it in a file. I've already tried this: 
def countwords(mytext):
    import codecs
    file = codecs.open(mytext, 'r', 'utf_8')
    count = 0
    mytext = file.readlines()
    for line in mytext:
       words = line.split()
         for word in words:
            count = count + 1
         file.close()
    return(count)
def CalculateMonoGram(path, lex): 
     fid = open(path, 'r', encoding='utf_8')
     mypath = fid.read().split()
     fid1 = open(lex, 'r', encoding='utf_8')
     mylex = fid1.read().split()
     for word1 in mylex:
         if word1 in mypath:
             x = dict((word1, mypath.count(word1)) for word1 in mylex)
         for value in x:
             monogram = '\t' + str(value / countwords(lex))
             table.write(monogram)
 
     
    