
def FindWordsEndingInSuffix(F, suff):
    # This returns a list of the words in F ending in suff
    L = []
    for w in F:
        word = w.strip()
        if word[-len(suff):] == suff:
            print word
            L.append(word)
    return L

def WriteListIntoFile(L, G):
    # This writes the list into file G, which must be already open
    for entry in L:
        G.write(entry + "\n")
    G.close()
    
def main():
    try:
        F = open("dict.txt", "r")
        print "I will find words ending in a suffix"
        suffix = raw_input("What suffix? " )
        L = FindWordsEndingInSuffix(F, suffix)
        print "I found %d such words" % len(L)
        if len(L) > 0:
            G = open("WordsWithSuffix_%s" % suffix, "w")
            WriteListIntoFile(L,G)
    except:
        print "Dictionary not found"

main()
