VOOZH about

URL: https://www.geeksforgeeks.org/python/python-program-to-read-file-word-by-word/

⇱ Python program to read file word by word - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python program to read file word by word

Last Updated : 16 Jan, 2026

Given a text file, read it word by word and process or display each word individually.

Example: (Myfile.txt)

👁 read-word-by-word-python

Output:
Geeks
4
geeks

Using split()

This method reads the file line by line and uses split() to break each line into individual words, making it a simple and efficient way to process words.

Output

Geeks
4
Geeks

Explanation:

  • for line in f: iterates over each line in the file
  • line.split(): splits the line into words using whitespace
  • for w in line.split(): loop over each word

Using a Generator Function

This method yields one word at a time while reading the file, making it memory-efficient and ideal for processing large files.

Output

Geeks
4
Geeks

Naive Manual Character-by-Character Approach

This method processes the file one character at a time without using Python’s built-in helpers, giving full control over how words and characters are counted.

Comment