Write the program to count the number of words in a text file. The program need
ID: 669349 • Letter: W
Question
Write the program to count the number of words in a text file. The program need to look at each line in the file and scan for whitespace. We need to consider only space and tab characters as whitespace, and consider a consecutive sequence of whitespace as a single block. The program need to skip and not count any line containing one whitespace block.
For example, the line below contains 4 words, and there are 4 blocks of whitespace (spaces and tab characters), but one of the whitespace blocks is at the start of the line.
Here is an apple.
By removing leading and trailing whitespace, counting the number of whitespace blocks and then adding 1, we get the number of words in one line of text.
Suppose the file sample.txt contains the following:
There are so many kinds of fruits.
Here is an apple.
But I don't like apples.
I like oranges but I don't like apples.
There ’s another word I feel about apples : @#%&
Then the program run produces the result shown below.
$ python3 findWord.py
Enter filename: sample.txt
32 words
The program must do:
• countWords( textFileName ) is a function that takes a file name and uses for loops and while loops to process the file’s content line-by-line, count the number of words in the file and print the count. A word is any non-empty sequence of non-whitespace characters.
• main() is a function that prompts the user for a file name and uses countWords to do
the work.
Constraints: You may not use any of the following python functions:
• str.partition()
• str.split()
Explanation / Answer
def countWords(fileName):
count = 0
with open(fileName) as fp:
for line in fp:
white = True
for ch in line:
if(ch != ' ' and ch != ' ' and ch != ' '):
if(white):
white = False
count += 1
else:
white = True
return count
def main():
fileName = str(input("Enter filename: "))
print(str(countWords(fileName)) + " words")
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.