- [Instructor] In this video,
let's talk about updating text files.
Now, let's assume that we have a record like 300 White 0.00
and what we wanna do in this case
is update the name White to the name Williams.
Now clearly Williams has more characters than White,
so in a typical file updating scenario,
you would locate the information that you want
and then you would overwrite the existing information
but you can only do that if every record
has fixed length fields.
So, for example, in a basic text file,
if I were to replace White with Williams,
I would actually wind up overwriting a portion
of the next field of data in the original record.
So, the typical way that you'll update a text file
is by creating a temporary file,
reading all of the records before the information
you wanna update and writing them
into the temporary file,
writing the updated record into the temporary file,
writing the rest of the original file
into the temporary file
and then using the temporary file
to overwrite the original.
So, that's what I'd like to demonstrate
in this video.
So, let's go ahead and switch over to a terminal Window
and for this purpose,
because we're going to need both
the original accounts.txt file
that we've been working with,
and a temporary file,
I'm actually going to create a couple
of statements here that open up those files
and now we can still use these files
in a with statement and still get the benefit
of closing those files automatically in a with statement
which is also what I'd like to talk about here.
So, first thing we're going to do
is open the accounts.txt file
and we're going to open it for reading
and separately, we're going to create a temporary file
called temp_file.txt
and we're going to open that one for writing.
Now let me go ahead and copy a with statement,
so we can talk about how the update
is going to occur.
So, as you can see here,
if you want to have more than one resource managed
by a with statement,
you can specify them in a comma-separated list
of values, in this case,
the variable names that we created previously
in the upper two snippets, snippets one and two
but we could have put these open expressions
into the with statement
if we had wanted to do that.
In that case, we would have said with open accounts.txt
for reading as accounts
and then we would have had a comma
and then open the next file
as temp_file and then the colon for the with statements,
to introduce the with statement's suite, excuse me.
So, now we have the two files
that we're going to manage
and we are going to use this for loop
to perform the process that I overviewed previously.
For every record in the accounts file,
we're going to break apart the current record
into its account name and balance.
We are going to check whether the specific account number
is 300 because we want to update White to Williams,
so if indeed the account number is not 300,
we will simply write the current full record out
to the temporary file,
otherwise we will create a new record
of information that we will write
into the temporary file.
So, as you can see, what we're doing here
is joining with the space character
as the separator a list
of values that have the same account number
that we had previously,
the new last name
and the balance,
so this will create the new record string
and then we'll simply write that new record
followed by a new line out to the file,
so of course when we execute that with statement,
it's going to perform the task
that we just reviewed,
it's then going to close the accounts
and temp_file files.
Now at this point, we have two files on disk,
the original accounts file
and the temp_file, so for example,
again, I can list out the contents
of those files, so I'll go ahead and again,
if you're a Windows user,
put the word more here instead of cat,
so this is the original file,
it still has White in it
and of course if I go ahead and bring up my temp_file,
you can see it's got Williams
which is the correct information,
so now what we wanna do is have this file overwrite
the proceeding file
and then we want to discard the temporary file
of information, so for this purpose,
we're going to take advantage
of the Python standard library's built-in module called os
which is for operating system commands
and the first thing we're going to do
is actually get rid of the accounts.txt file
and then we're going to rename
the temporary file to accounts.txt.
So, there's a function called remove
in the os module, you give it the name
of the file to remove and again,
if you don't specify path information,
it's assuming that the file is in the current folder
from which we're working,
so now we've deleted accounts.txt
and I can go ahead and list out the contents
or if you're a Windows user,
do a dir instead
and you can see there's no text file named accounts.txt
in the current folder anymore.
Then I can go ahead
and rename the temporary file
using the rename function from the os module,
so here's the file that we want to rename,
here is the new name for that file.
Now if I list out the directory contents
and again Windows users should use dir,
you can see indeed we have accounts.txt
and we no longer have the temporary file listed
in the current folder,
so at this point, we can go ahead
and list out the contents of the accounts.txt file
and indeed it has the updated last name
for the record 300 that we had modified up above.