Skip to main content

How to count the number of words in a file in Python

How to count the number of words in a file in Python.

Here is a step-by-step tutorial on how to count the number of words in a file using Python:

Step 1: Open the File To start, you need to open the file that you want to count the words in. You can do this using the open() function in Python. Specify the file path and the mode in which you want to open the file, such as 'r' for reading.

file_path = 'path_to_file.txt'
file = open(file_path, 'r')

Step 2: Read the File Contents Next, you need to read the contents of the file. You can do this using the read() method of the file object. This method reads the entire file and returns its contents as a string.

file_contents = file.read()

Step 3: Close the File After reading the file contents, it's good practice to close the file using the close() method. This releases any system resources used by the file.

file.close()

Step 4: Count the Words Now that you have the file contents as a string, you can count the number of words. There are multiple approaches you can take to count the words. Here are a few examples:

Example 1: Using the split() Method You can split the string into a list of words using the split() method. By default, this method splits the string at each whitespace character, which effectively separates the words. Then, you can use the len() function to get the count of words in the list.

words = file_contents.split()
word_count = len(words)

Example 2: Using Regular Expressions If you want to consider more complex word boundaries, you can use regular expressions. The re module in Python provides powerful regex functionality. The following example counts words, considering word boundaries as non-whitespace characters.

import re

words = re.findall(r'\b\w+\b', file_contents)
word_count = len(words)

Step 5: Print or Use the Word Count Finally, you can print the word count or use it for further processing according to your requirements.

print("Number of words in the file:", word_count)

That's it! You now know how to count the number of words in a file using Python. Remember to replace 'path_to_file.txt' with the actual path to your file.