Reading from a file

food.txt
.Apples
Oranges
Bananas
Tomatoes
Cucumber
Carrots
Peas
Create a new program and save it as food_read.py
.This program will read the contents of the file food.txt
and print it to the screen.Just as in the previous step, open the file and use f
as a reference to work with the file.Note: You need to use ‘read’ mode by using the parameter r
instead of w
(write) or a
(append).f = open("food.txt","r")
Next, read the contents of the file into a variable called data
and print it.data = f.read()
print(data)
Don’t forget to close the file.f.close()
Leaving files open can cause data loss and can also prevent access when files are used by multiple concurrent users.Run the program. Python will print the list all at once as a multi-line string showing each item on its own line./Images/1_5_food_read.png)
Iterating over the file contents
Sometimes it can be useful to iterate over the contents of a file, reading the contents line by line.The file is opened in the same manner as before.f = open("food.txt", "r")
Using a for
loop, the contents of the file f
can be read line by line. Each time through the loop a new line from the file is stored in the variable line
and then printed to the screen.for line in f:
print(line)
Tip: for
loops are loops that iterate a set number of times. This code will read each line from the file until it reaches the end of the file, when the for
loop terminates.Once again, it is important to close the file.f.close()
Using a with
statement
Notice how you have to close the file each time your program finishes. Ensuring that the file is opened and closed correctly is good file management as it reduces the risk of data loss. This is good practice and Python has built-in functions to simplify this.Using a with
statement you can open a file, do whatever you want with it, and then automatically close it.The with
statement also provides a means to manage any errors, for example if the file is not present or the file name is incorrect. It also provides automated cleanup, ensuring that a file is closed correctly when it is no longer needed.This program will open a file and store it in the variable f
, read the contents of the file to the variable data
, and then print it to the screen.with open("test.txt") as f:
data = f.read()
print(data)
Note: No code is needed to close the file, as the with
statement handles closing the file once the contents have been read.In the comments section discuss which data type data
is, and if you can think of any situations where you might not want this to be the case.Programming 103: Saving and Structuring Data

Our purpose is to transform access to education.
We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.
We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.
Learn more about how FutureLearn is transforming access to education