1 00:00:00,790 --> 00:00:01,623 - [Instructor] In this video, 2 00:00:01,623 --> 00:00:04,390 let's talk about updating text files. 3 00:00:04,390 --> 00:00:09,390 Now, let's assume that we have a record like 300 White 0.00 4 00:00:10,240 --> 00:00:12,070 and what we wanna do in this case 5 00:00:12,070 --> 00:00:15,640 is update the name White to the name Williams. 6 00:00:15,640 --> 00:00:19,070 Now clearly Williams has more characters than White, 7 00:00:19,070 --> 00:00:22,370 so in a typical file updating scenario, 8 00:00:22,370 --> 00:00:24,940 you would locate the information that you want 9 00:00:24,940 --> 00:00:27,750 and then you would overwrite the existing information 10 00:00:27,750 --> 00:00:31,070 but you can only do that if every record 11 00:00:31,070 --> 00:00:33,040 has fixed length fields. 12 00:00:33,040 --> 00:00:35,590 So, for example, in a basic text file, 13 00:00:35,590 --> 00:00:38,150 if I were to replace White with Williams, 14 00:00:38,150 --> 00:00:41,310 I would actually wind up overwriting a portion 15 00:00:41,310 --> 00:00:44,680 of the next field of data in the original record. 16 00:00:44,680 --> 00:00:47,620 So, the typical way that you'll update a text file 17 00:00:47,620 --> 00:00:50,200 is by creating a temporary file, 18 00:00:50,200 --> 00:00:53,150 reading all of the records before the information 19 00:00:53,150 --> 00:00:55,130 you wanna update and writing them 20 00:00:55,130 --> 00:00:57,130 into the temporary file, 21 00:00:57,130 --> 00:01:00,410 writing the updated record into the temporary file, 22 00:01:00,410 --> 00:01:02,500 writing the rest of the original file 23 00:01:02,500 --> 00:01:03,940 into the temporary file 24 00:01:03,940 --> 00:01:06,320 and then using the temporary file 25 00:01:06,320 --> 00:01:08,960 to overwrite the original. 26 00:01:08,960 --> 00:01:11,070 So, that's what I'd like to demonstrate 27 00:01:11,070 --> 00:01:12,700 in this video. 28 00:01:12,700 --> 00:01:15,780 So, let's go ahead and switch over to a terminal Window 29 00:01:15,780 --> 00:01:16,980 and for this purpose, 30 00:01:16,980 --> 00:01:18,880 because we're going to need both 31 00:01:18,880 --> 00:01:21,610 the original accounts.txt file 32 00:01:21,610 --> 00:01:23,020 that we've been working with, 33 00:01:23,020 --> 00:01:25,430 and a temporary file, 34 00:01:25,430 --> 00:01:27,690 I'm actually going to create a couple 35 00:01:27,690 --> 00:01:31,290 of statements here that open up those files 36 00:01:31,290 --> 00:01:33,770 and now we can still use these files 37 00:01:33,770 --> 00:01:36,690 in a with statement and still get the benefit 38 00:01:36,690 --> 00:01:40,360 of closing those files automatically in a with statement 39 00:01:40,360 --> 00:01:42,710 which is also what I'd like to talk about here. 40 00:01:42,710 --> 00:01:44,020 So, first thing we're going to do 41 00:01:44,020 --> 00:01:46,540 is open the accounts.txt file 42 00:01:46,540 --> 00:01:48,600 and we're going to open it for reading 43 00:01:48,600 --> 00:01:52,290 and separately, we're going to create a temporary file 44 00:01:52,290 --> 00:01:54,930 called temp_file.txt 45 00:01:54,930 --> 00:01:57,750 and we're going to open that one for writing. 46 00:01:57,750 --> 00:02:00,790 Now let me go ahead and copy a with statement, 47 00:02:00,790 --> 00:02:04,300 so we can talk about how the update 48 00:02:04,300 --> 00:02:05,620 is going to occur. 49 00:02:05,620 --> 00:02:07,290 So, as you can see here, 50 00:02:07,290 --> 00:02:11,090 if you want to have more than one resource managed 51 00:02:11,090 --> 00:02:12,680 by a with statement, 52 00:02:12,680 --> 00:02:16,520 you can specify them in a comma-separated list 53 00:02:16,520 --> 00:02:19,480 of values, in this case, 54 00:02:19,480 --> 00:02:22,640 the variable names that we created previously 55 00:02:22,640 --> 00:02:25,930 in the upper two snippets, snippets one and two 56 00:02:25,930 --> 00:02:29,050 but we could have put these open expressions 57 00:02:29,050 --> 00:02:31,410 into the with statement 58 00:02:31,410 --> 00:02:33,350 if we had wanted to do that. 59 00:02:33,350 --> 00:02:38,350 In that case, we would have said with open accounts.txt 60 00:02:38,580 --> 00:02:41,330 for reading as accounts 61 00:02:41,330 --> 00:02:43,470 and then we would have had a comma 62 00:02:43,470 --> 00:02:45,930 and then open the next file 63 00:02:45,930 --> 00:02:50,930 as temp_file and then the colon for the with statements, 64 00:02:51,110 --> 00:02:53,950 to introduce the with statement's suite, excuse me. 65 00:02:53,950 --> 00:02:55,450 So, now we have the two files 66 00:02:55,450 --> 00:02:56,630 that we're going to manage 67 00:02:56,630 --> 00:02:59,250 and we are going to use this for loop 68 00:02:59,250 --> 00:03:02,670 to perform the process that I overviewed previously. 69 00:03:02,670 --> 00:03:07,100 For every record in the accounts file, 70 00:03:07,100 --> 00:03:10,650 we're going to break apart the current record 71 00:03:10,650 --> 00:03:12,930 into its account name and balance. 72 00:03:12,930 --> 00:03:16,780 We are going to check whether the specific account number 73 00:03:16,780 --> 00:03:20,950 is 300 because we want to update White to Williams, 74 00:03:20,950 --> 00:03:25,290 so if indeed the account number is not 300, 75 00:03:25,290 --> 00:03:28,450 we will simply write the current full record out 76 00:03:28,450 --> 00:03:30,510 to the temporary file, 77 00:03:30,510 --> 00:03:33,800 otherwise we will create a new record 78 00:03:33,800 --> 00:03:36,340 of information that we will write 79 00:03:36,340 --> 00:03:37,930 into the temporary file. 80 00:03:37,930 --> 00:03:39,650 So, as you can see, what we're doing here 81 00:03:39,650 --> 00:03:42,850 is joining with the space character 82 00:03:42,850 --> 00:03:44,910 as the separator a list 83 00:03:44,910 --> 00:03:47,680 of values that have the same account number 84 00:03:47,680 --> 00:03:49,200 that we had previously, 85 00:03:49,200 --> 00:03:50,910 the new last name 86 00:03:50,910 --> 00:03:51,880 and the balance, 87 00:03:51,880 --> 00:03:54,400 so this will create the new record string 88 00:03:54,400 --> 00:03:57,080 and then we'll simply write that new record 89 00:03:57,080 --> 00:03:59,830 followed by a new line out to the file, 90 00:03:59,830 --> 00:04:02,470 so of course when we execute that with statement, 91 00:04:02,470 --> 00:04:04,480 it's going to perform the task 92 00:04:04,480 --> 00:04:05,850 that we just reviewed, 93 00:04:05,850 --> 00:04:08,511 it's then going to close the accounts 94 00:04:08,511 --> 00:04:10,990 and temp_file files. 95 00:04:10,990 --> 00:04:14,130 Now at this point, we have two files on disk, 96 00:04:14,130 --> 00:04:16,430 the original accounts file 97 00:04:16,430 --> 00:04:18,990 and the temp_file, so for example, 98 00:04:18,990 --> 00:04:21,960 again, I can list out the contents 99 00:04:21,960 --> 00:04:25,590 of those files, so I'll go ahead and again, 100 00:04:25,590 --> 00:04:27,080 if you're a Windows user, 101 00:04:27,080 --> 00:04:29,600 put the word more here instead of cat, 102 00:04:29,600 --> 00:04:31,060 so this is the original file, 103 00:04:31,060 --> 00:04:32,700 it still has White in it 104 00:04:32,700 --> 00:04:37,120 and of course if I go ahead and bring up my temp_file, 105 00:04:37,120 --> 00:04:38,660 you can see it's got Williams 106 00:04:38,660 --> 00:04:40,190 which is the correct information, 107 00:04:40,190 --> 00:04:43,530 so now what we wanna do is have this file overwrite 108 00:04:43,530 --> 00:04:44,570 the proceeding file 109 00:04:44,570 --> 00:04:48,170 and then we want to discard the temporary file 110 00:04:48,170 --> 00:04:50,460 of information, so for this purpose, 111 00:04:50,460 --> 00:04:52,350 we're going to take advantage 112 00:04:52,350 --> 00:04:56,820 of the Python standard library's built-in module called os 113 00:04:56,820 --> 00:04:59,920 which is for operating system commands 114 00:04:59,920 --> 00:05:02,440 and the first thing we're going to do 115 00:05:02,440 --> 00:05:07,020 is actually get rid of the accounts.txt file 116 00:05:07,020 --> 00:05:09,161 and then we're going to rename 117 00:05:09,161 --> 00:05:13,130 the temporary file to accounts.txt. 118 00:05:13,130 --> 00:05:15,010 So, there's a function called remove 119 00:05:15,010 --> 00:05:17,320 in the os module, you give it the name 120 00:05:17,320 --> 00:05:19,550 of the file to remove and again, 121 00:05:19,550 --> 00:05:21,900 if you don't specify path information, 122 00:05:21,900 --> 00:05:24,960 it's assuming that the file is in the current folder 123 00:05:24,960 --> 00:05:26,330 from which we're working, 124 00:05:26,330 --> 00:05:29,010 so now we've deleted accounts.txt 125 00:05:29,010 --> 00:05:31,980 and I can go ahead and list out the contents 126 00:05:31,980 --> 00:05:33,270 or if you're a Windows user, 127 00:05:33,270 --> 00:05:35,131 do a dir instead 128 00:05:35,131 --> 00:05:39,670 and you can see there's no text file named accounts.txt 129 00:05:39,670 --> 00:05:41,690 in the current folder anymore. 130 00:05:41,690 --> 00:05:42,820 Then I can go ahead 131 00:05:42,820 --> 00:05:46,860 and rename the temporary file 132 00:05:46,860 --> 00:05:49,890 using the rename function from the os module, 133 00:05:49,890 --> 00:05:52,420 so here's the file that we want to rename, 134 00:05:52,420 --> 00:05:54,700 here is the new name for that file. 135 00:05:54,700 --> 00:05:57,040 Now if I list out the directory contents 136 00:05:57,040 --> 00:06:00,020 and again Windows users should use dir, 137 00:06:00,020 --> 00:06:02,780 you can see indeed we have accounts.txt 138 00:06:02,780 --> 00:06:06,600 and we no longer have the temporary file listed 139 00:06:06,600 --> 00:06:08,000 in the current folder, 140 00:06:08,000 --> 00:06:10,740 so at this point, we can go ahead 141 00:06:10,740 --> 00:06:15,570 and list out the contents of the accounts.txt file 142 00:06:15,570 --> 00:06:18,810 and indeed it has the updated last name 143 00:06:18,810 --> 00:06:23,203 for the record 300 that we had modified up above.