Python program to read text file line by line and store it into an array with Practical Example
Program Logic:
- Open any text file in read mode using open function and pass filename and access mode as an arguments to it
- Create array object
- Read text file line by line and store it into another variable say ‘data’
- Use split method to convert line into words and store it in new variable say ‘l’
- Use FOR loop to iterate through word by word
- Use append method of array object within FOR loop and write word of file into it
- Display array object using print function
Below is implementation code/Source
fin = open("book.txt","r")
array = []
data = fin.read()
l = data.split()
for line in l:
array.append(line)
print(array)
Below is Output:
['Python', 'is', 'open', 'source', 'programming', 'language', 'python', 'is', 'interpreted', 'language', 'how', 'are', 'you', 'python', 'is', 'used', 'for', 'creating', 'different', 'types', 'of', 'networking', 'and', 'desktop', 'application']
Below is Executable code with output:


Below is book.txt file


<