'read from a file on demand in python

I would like to read a text file word by word when I need it. Like the ifstream in C++. I mean, I want to open the file, then I read the next word from it when I need it, and then close it. How do I do that?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source