read
Let us continue working with examples.txt
that we created in the previous lesson. If you recall, examples.txt
has the following contents:
xxxxxxxxxx
51one
2two
3three
4four
5five
Let us now look at a different way of reading from a file, using the read
method.
xxxxxxxxxx
41f = open('examples.txt', 'r')
2content = f.read()
3print(content)
4f.close()
This gives the following output:
xxxxxxxxxx
51one
2two
3three
4four
5five
read
is a method defined for the file object. When it is called without any argument, it returns a string that contains the entire content of the file. If you head to the console (it is to the right of the editor in Replit) and type the string content
, this is what you get:
xxxxxxxxxx
11'one\ntwo\nthree\nfour\nfive'
Notice that content
is a single string. It contains the contents of the file, but between consecutive lines in the file, there is a \n
or a newline character:
Except for the last line, every line in the file ends with a \n
character. When this string is printed to the console — print(content)
— we get five separate lines even though we are only passing a single string to the print
function. This is because of the presence of the newline character in the string. Whenever a newline character is encountered, the Python interpreter moves to the next line.
Now, it is clear why the following piece of code printed an extra line between consecutive lines in the file:
xxxxxxxxxx
61f = open('examples.txt', 'r')
2for line in f:
3 print(line)
4 # line ends with a \n character for all lines except the last one
5 # this is why we get an empty line between consecutive lines in the console
6f.close()
readline
As its name suggests, the readline
method reads from the file one line at a time:
xxxxxxxxxx
71f = open('examples.txt', 'r')
2line1 = f.readline()
3line2 = f.readline()
4line3 = f.readline()
5line4 = f.readline()
6line5 = f.readline()
7f.close()
The variables line1
, line2
, …, line5
will hold the following values at the end of execution of the code given above:
Variable | Value |
---|---|
line1 | 'one\n' |
line2 | 'two\n' |
line3 | 'three\n' |
line4 | 'four\n' |
line5 | 'five' |
Notice that line5
doesn't have a \n
at the end as it is the last line in the file. Here, we know that there are five lines in the file. This helped us define five separate variables. But what if there are more lines? Generally, we read a file so as to see what its contents are because we don't know what is there in it. Clearly, we need a way to figure out when the file ends.
Now, consider the following code. What happens if we try to read the file using readline
after all the lines in the file have been read?
xxxxxxxxxx
81f = open('examples.txt', 'r')
2line1 = f.readline()
3line2 = f.readline()
4line3 = f.readline()
5line4 = f.readline()
6line5 = f.readline()
7line = f.readline()
8f.close()
If we execute this and head to the console, we see that the variable line
defined in line-7 is an empty string! This gives us a way to determine when a file is empty:
Keep reading lines from the file until an empty string is encountered.
Let us implement this:
xxxxxxxxxx
61f = open('examples.txt', 'r')
2line = f.readline()
3while line != '':
4 print(line, end = '')
5 line = f.readline()
6f.close()
Here, we have managed to read the file using just one string variable. Let us make few more changes to this code:
xxxxxxxxxx
61f = open('examples.txt', 'r')
2line = f.readline()
3while line:
4 print(line.strip())
5 line = f.readline()
6f.close()
In this code, we have made two changes. One in line-3 and another in line-4. The loop condition in line-3 checks for the empty string. If line
is an empty string, it evaluates to False
and the loop will be terminated. This is a compact way of writing line != ''
. Python treats empty sequences as False
. If this is confusing, execute the following code and check the output:
xxxxxxxxxx
31line = ''
2if not line:
3 print('It works!')
In line-4, we are using the strip
method to strip the string line
of all the whitespace characters at the beginning and at the end. In this way, the trailing newline at the end of line
will be stripped. This way, we don't need to use the end
argument.
readlines
Finally, Python also provides a way to read the file and store it as a list of lines:
xxxxxxxxxx
51f = open('examples.txt', 'r')
2lines = f.readlines()
3for line in lines:
4 print(line.strip())
5f.close()
Here, lines
is a list of lines. Notice that each element in lines
corresponds to one line in the file. It is always a string:
xxxxxxxxxx
11['one\n', 'two\n', 'three\n', 'four\n', 'five']
write
We already saw the write
method earlier. There, we used the write
method five times to write five lines. Let us now use a loop with the help of the lines
list. First, we run the code:
xxxxxxxxxx
51f = open('writing.txt', 'w')
2lines = ['one', 'two', 'three', 'four', 'five']
3for line in lines:
4 f.write(line + '\n')
5f.close()
When we execute this, the following file is created:
We see that there are six lines in the file and not five, though we seem to have written only five lines. The problem is with line-4, where we are adding \n
after every string in the list lines
. We should make sure that we don't add a \n
after the last string in the list:
xxxxxxxxxx
91f = open('writing.txt', 'w')
2lines = ['one', 'two', 'three', 'four', 'five']
3for i in range(len(lines)):
4 line = lines[i]
5 if i != len(lines) - 1:
6 f.write(line + '\n')
7 else:
8 f.write(line)
9f.close()
Now, check the file, you will see that it has exactly five lines! Let us now try to write an integer to the file:
xxxxxxxxxx
31f = open('writing.txt', 'w')
2f.write(1)
3f.close()
This throws the following error:
xxxxxxxxxx
41Traceback (most recent call last):
2File "main.py", line 2, in <module>
3f.write(1)
4TypeError: write() argument must be str, not int
We see that write
method accepts only string arguments. If we want to write integers to a file, we have to first convert them to strings:
xxxxxxxxxx
31f = open('writing.txt', 'w')
2f.write(str(1))
3f.close()
As an exercise, try to run the following code. What do you observe? Why do you think this happens?
xxxxxxxxxx
31f = open('writing.txt', 'w')
2f.writeline(str(1))
3f.close()
writelines
We can write a list of lines to a file using the writelines
method:
xxxxxxxxxx
41f = open('writing.txt', 'w')
2lines = ['1\n', '2\n', '3\n', '4\n', '5']
3f.writelines(lines)
4f.close()
Note that the argument passed to the writelines
method is a list of strings. This will create a file having the following contents:
xxxxxxxxxx
511
22
33
44
55