Skip to content
Advertisement

How to find how many words are in a file [closed]

I’m trying to make something to let you choose a bot’s WPM in a type racing game so let’s say you have a sentence like

for car owners, transmission failure can be a complete nightmare, costing thousands of dollars to fix or replace. 

I need to find out how many words there are in it and the words can include periods and comma’s like

for,

the comma can be included in the one word so that is just one word. And I need it to be able to read this sentence from a .txt file and give me the word count so I can calculate the delay needed to get the desired WPM. I have no idea how to do that so any help would be appreciated.

Advertisement

Answer

As mentioned in a comment by @S3DEV:

Once you have read the text file into a string s, you can do len(s.split()) to find the amount of words!

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement