- [Instructor] Of course, another thing
that you'll want to do in Python
is be able to read the contents of a file into your program
for processing purposes.
So, let's take a look at a with statement
that opens a file for reading.
Now, as you can see here,
at the beginning of this with statement,
we are once again using the built-in open function
to open up the accounts.txt file that we previously created
that had five fake bank accounts in it.
And, in this case, we are opening the file for reading.
The mode r is opening a text file for reading.
And this open command will work
only if the file already exists
and can be found at the specified location.
Now, because I'm not specifying a location here,
it is going to assume the file is in the current folder
from which I am executing iPython at the moment.
So, indeed it is, as I showed you in a preceding video.
So we're gonna open up that file for reading
and we're going to assign it to the variable accounts
so that we can use that in the sweep below.
Now, let me go ahead and execute this first
just so you can see the results.
As you can see, we have three columns of information
and we've formatted the data in those columns
to be left-aligned in the first two columns
and right-aligned in the third column.
So first of all, we printed the column heads
and we only did that if the file was opened correctly.
If the file is not opened correctly
because, for example, the file doesn't exist,
an exception would have occurred
and the with statement would have terminated immediately.
So, by putting the print statement in the with statement,
it will be displayed only if we successfully
opened the file.
Now, as you can see, we're printing a formatted string,
an f string.
And in the first placeholder,
we have the string Account left-aligned in a field of 10.
In the second placeholder,
we have the string Name left-aligned in a field of 10.
And in the last column,
we have the string Balance right-aligned in a field of 10.
And that's going to enable us to right-align
all of the numeric values and line up their decimal points.
Now, the important piece in the body
of the with statement here is this for loop,
which is iterating through the file object that we created.
One of the interesting things about file objects in Python
is that they are iterable.
And in the case of a text file,
this automatically reads one line at a time from the file.
So, for each of the records in accounts,
for each line of text in the accounts file object,
we are going to tell that string to split,
which will give me a list of the pieces of information
which were separated by spaces
in the actual records of text that were written to the file
in the first place.
We will then unpack that list, which has three values in it,
into account, name, and balance
which is the specific order
in which each record of information was written.
First, we had an account number.
Then a last name.
Then a balance.
And finally, we will then print out that set of information
for the current record
and, once again, we're using an f string.
So the account number will be left-aligned in a field of 10,
underneath the string Account.
The name value will be left-aligned in a field of 10,
under the column head Name.
And the balance value will be right-aligned
in a field of 10.
And remember, these are all string pieces of information,
so I'm not using floating-point formatting here.
I'm simply redisplaying the text
that was read from the file.
If we had subsequently read the text
and converted it into a numeric value,
only then would I use numeric formatting
in this particular case.
Now what's going to happen to this for loop
is it will keep reading from the file
until the end of file indicator
is emitted from reading from the file.
So that's taken care of for you
at the operating system level,
and, once that happens,
the for loop automatically terminates.
And once the for loop terminates,
the with statement will terminate
and automatically close the accounts.txt file.
Now, you may be wondering whether you have to iterate
one line at a time through a text file.
It turns out that you don't.
There is a method built into the file object
called read lines,
which will read the entire text file
and give you back a list of strings
representing the individual lines of text within the file.
However, keep in mind that if the text file
is extremely large, that could take time
and could take up a lot of memory, as well.
Whereas iterating one line at a time through the text file
is accessing the data on demand, if you will,
giving you only one string at a time from the file,
enabling you to process that
and then basically forget about it at that point
and move on to the next record.