Let's test your knowledge. Fill in the missing part by typing it in.
To read data from a file, we can use the open()
function in Python. The open()
function takes two arguments: the name of the file and the mode ('r' for read mode). Here's an example:
PYTHON
1# Open the file in ___________ mode
2file = open('data.txt', 'r')
3
4# Read the contents of the file
5content = file.read()
6
7# Close the file
8file.close()
9
10# Print the contents of the file
11print(content)
In the example above, we use the open()
function to open the file named 'data.txt' in read mode.
Write the missing line below.