1 00:00:01,130 --> 00:00:02,760 - [Instructor] In this self check exercise, 2 00:00:02,760 --> 00:00:07,150 we'd like you to create your own text file called grades.txt 3 00:00:07,150 --> 00:00:09,810 and we'd like you to write three records of information 4 00:00:09,810 --> 00:00:12,590 consisting of a fake student ID, 5 00:00:12,590 --> 00:00:14,420 a last name, and a letter grade. 6 00:00:14,420 --> 00:00:17,090 So here's some sample records 7 00:00:17,090 --> 00:00:19,180 that you can write into the file. 8 00:00:19,180 --> 00:00:23,150 So go ahead and pause the video and use a with statement 9 00:00:23,150 --> 00:00:25,520 to actually create the file 10 00:00:25,520 --> 00:00:27,410 like we did in the preceding video. 11 00:00:27,410 --> 00:00:28,860 When you're done doing that, 12 00:00:28,860 --> 00:00:30,613 come back here to see my answer. 13 00:00:35,720 --> 00:00:37,890 Okay, so all we're going to do, 14 00:00:37,890 --> 00:00:39,530 like we did in the preceding video, 15 00:00:39,530 --> 00:00:44,110 is open, in a with statement, the grades.txt file 16 00:00:44,110 --> 00:00:46,870 which will be written into the current folder 17 00:00:46,870 --> 00:00:50,040 wherever you are executing the statement. 18 00:00:50,040 --> 00:00:52,010 We're going to open that file for writing. 19 00:00:52,010 --> 00:00:54,680 So, again, if that file already exists, 20 00:00:54,680 --> 00:00:56,970 it will be overwritten by this 21 00:00:56,970 --> 00:01:00,570 with statement opening the grades.txt file. 22 00:01:00,570 --> 00:01:03,190 We're going to treat the resulting file object 23 00:01:03,190 --> 00:01:06,010 as the name grades, the variable name grades, 24 00:01:06,010 --> 00:01:08,320 or interact with it through that variable. 25 00:01:08,320 --> 00:01:12,330 And here we're writing out the three records of information 26 00:01:12,330 --> 00:01:15,140 we specified in the problem statement. 27 00:01:15,140 --> 00:01:16,800 So we'll go ahead and execute that. 28 00:01:16,800 --> 00:01:18,990 And, again, depending on whether 29 00:01:18,990 --> 00:01:21,630 you're a macOS or Windows user, 30 00:01:21,630 --> 00:01:24,610 you can execute the exclamation point cat 31 00:01:24,610 --> 00:01:27,150 or exclamation point more command 32 00:01:27,150 --> 00:01:30,330 followed by the name of your file to see its contents. 33 00:01:30,330 --> 00:01:32,890 So let me go ahead and do that here on my Mac, 34 00:01:32,890 --> 00:01:34,010 and as you can see, 35 00:01:34,010 --> 00:01:36,400 indeed the file was created 36 00:01:36,400 --> 00:01:38,440 with the three records of information 37 00:01:38,440 --> 00:01:41,543 that we wrote in the with statements suite.