SuperAdmin
SuperAdmin

Reputation: 548

Counting words in a text file via python

Below is a project I have been assigned in which I must write a program that opens a text file and counts the words in the file. Below I have outlined a program that is supposed to set previous = False for any cases of white space (ie. spaces, tabs, \n) and sets previous = True for any other case. When there exists a case where previous = False and there is no white space detected (beginning of a word), it will add 1 to the wordCount. However, my output shows slightly different results (shown below). Restrictions are I cant use .split() functions, must be done by hand. Being this is a school assignment, I'm not looking for someone to do this for me, but merely teach me a bit and explain what I'm doing wrong.

Code:

"""
title: findWords.py
author: Riley Lloyd
"""

#import necessary libraries
import turtle as bob

def init():
    """

    :return:
    """
    pass

def countWords ( textFileName ):
    """

    :param textFileName:
    :return:
    """
    previous = False
    wordCount = 0
    for text in open(textFileName):
        print(text)
        if text == " ":
            previous = False
        elif text == "\n":
            previous = False
        elif text == "\t":
            previous = False
        else:
            if previous == False:
                wordCount += 1
            previous = True
    print(wordCount)


def main():
    """

    :return:
    """
    init()
    countWords(input("Enter filename: "))

main()

Results:

Enter filename: some.txt
Words make up other words.

This is a line.

  Sequences of words make sentences.

I like words but I don't like MS Word.

    There's another word for how I feel about MSWord: @#%&

1

Process finished with exit code 0

Upvotes: 1

Views: 176

Answers (1)

Anand S Kumar
Anand S Kumar

Reputation: 90889

You are iterating over the open file -

for text in open(textFileName):

When you do that, you are actually iterating over the lines of the file , so in first iteration text would be the first line of the file, in second iteration text would be the second line of the file, etc. But, your logic is written such that it expects text would be a each character in the file.

If your file is not large I would suggest you to do .read() and iterate over that. Example -

def countWords ( textFileName ):
    """

    :param textFileName:
    :return:
    """
    with open(textFileName) as f:
        texts = f.read()
        previous = False
        wordCount = 0
        for text in texts:
            print(text)
            if text == " ":
                previous = False
            elif text == "\n":
                previous = False
            elif text == "\t":
                previous = False
            else:
                if previous == False:
                    wordCount += 1
                previous = True
        print(wordCount)

I have used with statement to open the file, you should also use with statements to open the file, it will automatically handle closing of the files for you.

Upvotes: 4

Related Questions