简体   繁体   中英

Count and print the number of words in a file

I am trying to write a program that count and print the number of words in a file. The problem is that I can't use functions like split() or partition() as we don't cover them yet.

def CountWords(TextfileName)
    for 

I don't know how to start my program, can anyone explain for me a little more about reading text file?

The program should look like this:

Suppose the file some.txt contains the following:

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: @#%&

Then the program run produces the result shown below.

CODE: SELECT ALL
$ python3 findWord.py
Enter filename: some.txt
33 words
with open('some.txt') as f:
   count = len(f.read().split())
print(count)

I am trying to write a program that count and print the number of words in a file. The problem is that I can't use functions like split() or partition() as we don't cover them yet.

def CountWords(TextfileName)
    for 

I don't know how to start my program, can anyone explain for me a little more about reading text file?

The program should look like this:

Suppose the file some.txt contains the following:

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: @#%&

Then the program run produces the result shown below.

CODE: SELECT ALL
$ python3 findWord.py
Enter filename: some.txt
33 words

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM