Python program to read file word by word
Prerequisites: File Handling in Python
Given a text file and the task is to read the information from file word by word in Python.
I am R2J!
Geeks 4 Geeks
And in that dream, we were flying.
- Open a file in read mode which contains a string.
forloop to read each line from the text file.
- Again use
forloop to read each word from the line splitted by ‘ ‘.
- Display each word from each line in the text file.
Example 1: Let’s suppose the text file looks like this –
Geeks 4 geeks
Example 2: Let’s suppose the text file contains more than one line.
Geeks 4 Geeks And in that dream, we were flying.