Write a python program to read text file line by line and display each word separated by #
In this post, i am going to write python program to read text file line by line and display each word separated by #. First, we will see program logic and then construct the program segments. We will create text file and store it in “F:/” drive and then read the content of file line by line
The following is book.txt text file


Program Logic:
- Open text file say ‘book.txt’ in read mode using open function
- Pass file name and access mode to open function.Here,we want to read text file that’s why access mode should be “r”.
- Read the whole content of text file using readlines() function and store it in another variable say ‘lines’
- Apply for loop to iterate through text file
- Use split function on line object and store words in variable say ‘words’. it splits a string into a list
- Iterate through word by word using for loop
- Specifies the separator “#”
- Display new modified contents of text file using print() function
Below is implementation code/Source code


Output:


Program segment 2
We can use join() function to attach separator to the word of text file . The following is textfile.txt. We will use this file to read contents and display word on output screen


Below is implementation code


output:


<