Program 2: Read a file line by line and print it

# Function to read files line by line
def ReadFile(filename):
   with open(filename, "r+") as file:
       lines = file.readlines()
       return lines


# Driver Program - Make sure you keep the txt file in the same directory where .py file is
filename = "test.txt"
lines = ReadFile(filename)
for i in lines:
   print(i.rstrip())

Clear Doubts with Computer Tutor
In case you’re facing problems in understanding concepts, writing programs, solving questions, want to learn fun facts | tips | tricks or absolutely anything around computer science, feel free to join CTs learner-teacher community: students.computertutor.in

You cannot copy content of this page