Let us use Replit
and use the Add File
button to create a file.
Each file should be given a name. Let us call our file examples.txt
. Now, we shall add the following lines to the file:
xxxxxxxxxx
51one
2two
3three
4four
5five
After creating the file, this is how it should look in Replit when we click on examples.txt
:
examples.txt
is called a text file. We can identify this from the extension — txt
that comes at the end of files. Don't worry too much about the extension. It is enough if you know that different files come with different extensions. In fact, main.py
is itself a file with py
as the extension. This is why it gets listed along with examples.txt
under the Files
tab in Replit.
Now, it is time to open the file and print the contents on to the console. For this, we head to main.py
and type the following lines.
xxxxxxxxxx
41f = open('examples.txt', 'r')
2for line in f:
3 print(line)
4f.close()
open
is a built-in function in Python that accepts two arguments:
The first argument is the file name, which is 'examples.txt'
in our case. The second argument corresponds to the mode in which we want to process the file. In this case, we want to read the file. So, we open the file in read-mode. The single character 'r'
is used to denote this mode. Notice that both arguments passed to open
are strings.
The open
function returns a file object. Do no worry about the terminology as yet. We will discuss it in detail in the next lesson. For now, it is enough to know that the open
function returns a file object that we have called f
in our code.
In lines 2-3, we loop through each line in the file and print it. As simple as that. Finally, in line-4, we close the file using the method close
. It is a good practice to close the file once we are done with processing it. Let us now see the output at the end of execution of this code block:
xxxxxxxxxx
91one
2
3two
4
5three
6
7four
8
9five
Seems interesting! We have all the contents of the file. But, for whatever reason, there is an extra line appearing between successive lines in the file. To suppress these new lines, we have to modify our print function slightly:
xxxxxxxxxx
41f = open('examples.txt', 'r')
2for line in f:
3 print(line, end = '') # there is NO SPACE between the quotes
4f.close()
Note the change in line-3. By default, print
appends a newline character (\n
) at the end of whatever it is printing. By using end = ''
, we are just appending the empty string. Therefore, the extra line that was appearing in the output will no longer bother us when we execute the code we have just written:
xxxxxxxxxx
51one
2two
3three
4four
5five
Consider the following code-block:
xxxxxxxxxx
71f = open('writing.txt', 'w')
2f.write('one ')
3f.write('two ')
4f.write('three ')
5f.write('four ')
6f.write('five')
7f.close()
Here, we have opened the file in write mode. When this code is executed, it creates a file in Replit called writing.txt
.
We have used what is called the write
method to write to the file. We pass the content we wish to write as a string argument to the method. Notice that, even though we have used the write
method to write five different words on five lines in the code, all of them get written to the same line in the file. The way to tell the file object to go to a new line is using the \n
character. Let us now, try the following piece of code:
xxxxxxxxxx
111f = open('writing.txt', 'w')
2f.write('one')
3f.write('\n')
4f.write('two')
5f.write('\n')
6f.write('three')
7f.write('\n')
8f.write('four')
9f.write('\n')
10f.write('five')
11f.close()
A better way of achieving this in fewer lines of code is to append the \n
character to every line of the file we wish to write:
xxxxxxxxxx
71f = open('writing.txt', 'w')
2f.write('one\n')
3f.write('two\n')
4f.write('three\n')
5f.write('four\')
6f.write('five')
7f.close()
This results in the same file but with fewer lines of code! In the next lesson, we will take a closer look at the idea of a file object.