Table of Contents
The task is to open a file and read all the words of that file.
with open('demo.txt','r') as txtfile: #Reading lines of file for readline in txtfile: #Read and print the words for str in readline.split(): print(str)
- To open a file in python, in-built function open() is used, it takes two arguments: the file name or path and mode (i.e. ‘r’ for read, ‘w’ for write etc).
- Read the lines of file and words using for loop and print the words.