import os
    import re
    import sys
    sys.stdout=open('f1.txt','w')
    from collections import Counter
    from glob import glob

    def removegarbage(text):
        text=re.sub(r'\W+',' ',text)
        text=text.lower()
        return text

    folderpath='d:/induvidual-articles'
    counter=Counter()


    filepaths = glob(os.path.join(folderpath,'*.txt'))

    num_files = len(filepaths)

    with open('topics.txt','r') as filehandle:
        lines = filehandle.read()
        words = removegarbage(lines).split()
       counter.update(words)


    for word, count in counter.most_common():
        probability=count//num_files
        print('{}  {} {}'.format(word,count,probability))

i am getting a zero division error:float division by zero
for the line
probability=count//num_files

how do i rectify it?

i need my output to be of the form:
word, count ,probability

Plz help!

  1. You get zerodivision error if num_files == 0. It is impossible to find any word in nonexistent files, so the probability should be zero. So you should write (and see next point)
    probability=count/num_files if numfiles else 0

  2. // is integer division in python.
    you should:
    python3: probability=count/num_files
    python2 or 3: probability=float(count)/num_files

see

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.