How to read to a file? | Python

/ #Python


In this guide, I will show you how to read a file, line by line using Python.

Yesterday, I wrote a short guide on how to write to a file using Python. Today, you will learn how to read this file.

the_file = open('the_file.txt', 'r') 
lines = the_file.readlines()

Now you have a variable called lines. This is a list of the lines. To print them, you can do this:

for line in lines:
  print(line)

Comments

No comments yet...

Add comment

Info

Please log in to comment!

Newsletter

Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.