1 00:00:00,780 --> 00:00:03,540 In this self-check exercise, I'd like you to try 2 00:00:03,540 --> 00:00:05,890 the update process that I presented to you 3 00:00:05,890 --> 00:00:07,370 in the preceding video. 4 00:00:07,370 --> 00:00:09,540 This time, looking specifically for 5 00:00:09,540 --> 00:00:13,230 the last name Doe and updating it to Smith. 6 00:00:13,230 --> 00:00:15,990 Now if by any chance you're switching from 7 00:00:15,990 --> 00:00:20,150 IPython into Jupiter to do the lab exercise 8 00:00:20,150 --> 00:00:23,070 I just want to point out that the Jupiter files 9 00:00:23,070 --> 00:00:26,290 are in a separate folder from where 10 00:00:26,290 --> 00:00:28,570 I've been demonstrating IPython. 11 00:00:28,570 --> 00:00:32,940 So for example I had to copy my accounts.txt file 12 00:00:32,940 --> 00:00:37,580 into the snippets_ipynb folder 13 00:00:37,580 --> 00:00:41,350 in order to demonstrate the solution to this exercise. 14 00:00:41,350 --> 00:00:44,200 So go ahead and try the exercise on your own, 15 00:00:44,200 --> 00:00:45,893 then come back to see the answer. 16 00:00:50,790 --> 00:00:52,960 Okay, let's go ahead and take a look. 17 00:00:52,960 --> 00:00:55,600 As you can see we're first going to open 18 00:00:55,600 --> 00:00:58,430 the accounts.txt file for reading. 19 00:00:58,430 --> 00:01:02,690 And we are going to open a temporary file for writing. 20 00:01:02,690 --> 00:01:05,970 And again we're going to write all of the content 21 00:01:05,970 --> 00:01:08,739 before the record that needs updating 22 00:01:08,739 --> 00:01:11,724 into the temporary file, then we're going to 23 00:01:11,724 --> 00:01:14,778 write the updated record into the temporary file. 24 00:01:14,778 --> 00:01:16,070 Then we're going to write the rest 25 00:01:16,070 --> 00:01:20,550 of the original accounts.txt file into the temporary file, 26 00:01:20,550 --> 00:01:23,510 and then finally we're going to delete the original file 27 00:01:23,510 --> 00:01:26,730 and rename the temporary file as well. 28 00:01:26,730 --> 00:01:29,280 So first let's do the writing process 29 00:01:29,280 --> 00:01:31,850 for which we have the with statement you see here. 30 00:01:31,850 --> 00:01:34,220 And again accounts and temp file 31 00:01:34,220 --> 00:01:36,930 will automatically be closed when 32 00:01:36,930 --> 00:01:38,920 the with statement terminates. 33 00:01:38,920 --> 00:01:41,130 For every record in the accounts file 34 00:01:41,130 --> 00:01:43,300 we're going to split apart the record 35 00:01:43,300 --> 00:01:45,690 into it's three pieces of information, 36 00:01:45,690 --> 00:01:49,360 looking specifically for the record that contains Doe. 37 00:01:49,360 --> 00:01:51,490 If the name does not contain Doe, 38 00:01:51,490 --> 00:01:54,680 then we will simply write the original record out. 39 00:01:54,680 --> 00:01:57,540 Otherwise we will create our new record 40 00:01:57,540 --> 00:02:00,610 by joining together, separated by spaces, 41 00:02:00,610 --> 00:02:03,890 the list of arguments consisting of 42 00:02:03,890 --> 00:02:05,680 account, Smith, and balance. 43 00:02:05,680 --> 00:02:08,890 And again notice that that's in square brackets there 44 00:02:08,890 --> 00:02:10,480 because the join method expects 45 00:02:10,480 --> 00:02:13,920 to receive as an argument a sequence object. 46 00:02:13,920 --> 00:02:15,600 So that'll create the new record, 47 00:02:15,600 --> 00:02:17,060 then we'll write it out to disk, 48 00:02:17,060 --> 00:02:20,270 If it was not the record that needed updating 49 00:02:20,270 --> 00:02:22,140 we'll keep writing out the other records 50 00:02:22,140 --> 00:02:25,110 until the file accounts has been 51 00:02:25,110 --> 00:02:27,230 fully read into memory. 52 00:02:27,230 --> 00:02:29,210 So let's go ahead and execute that. 53 00:02:29,210 --> 00:02:32,780 Now we have our temporary file set up, 54 00:02:32,780 --> 00:02:34,980 and so the next thing we're going to do 55 00:02:34,980 --> 00:02:38,600 is use the operating system module OS 56 00:02:38,600 --> 00:02:43,010 to enable us to remove the original record, 57 00:02:43,010 --> 00:02:46,280 I'm sorry original file, accounts.txt 58 00:02:46,280 --> 00:02:48,300 so now that no longer exists. 59 00:02:48,300 --> 00:02:51,380 Then we're going to rename the original file. 60 00:02:51,380 --> 00:02:54,180 And finally, so that we can check that 61 00:02:54,180 --> 00:02:58,090 the original file does exist and has been updated, 62 00:02:58,090 --> 00:02:59,940 we're going to list out its contents 63 00:02:59,940 --> 00:03:02,740 using a command line command. 64 00:03:02,740 --> 00:03:05,080 Now for those of you on Mac OS and lenox 65 00:03:05,080 --> 00:03:08,050 you can use this first command cat, 66 00:03:08,050 --> 00:03:10,430 otherwise you can use more on Windows. 67 00:03:10,430 --> 00:03:15,320 And you can see that indeed we have the updated record. 68 00:03:15,320 --> 00:03:19,130 And now instead of Doe for record 200 69 00:03:19,130 --> 00:03:20,800 we have the name Smith. 70 00:03:20,800 --> 00:03:24,530 And you can go back and confirm previously 71 00:03:24,530 --> 00:03:27,883 that we had the name Doe in that record.