WEBVTT 1 00:00:02.320 --> 00:00:06.420 So in the previous two videos, we learned about reading and writing to text files. 2 00:00:06.420 --> 00:00:08.850 So they're the two modes that you would use, probably, fairly often. 3 00:00:08.850 --> 00:00:13.290 And we can also append to a text file by using an A as the mode for append. 4 00:00:13.290 --> 00:00:17.220 Now, opening a text file in append mode causes data to be written to 5 00:00:17.220 --> 00:00:18.490 the end of the file. 6 00:00:18.490 --> 00:00:20.900 Without overriding the existing contents. 7 00:00:20.900 --> 00:00:24.000 In fact these three modes are short for RT, WT, and AT. 8 00:00:24.000 --> 00:00:25.570 Or TR, TW, and TA. 9 00:00:25.570 --> 00:00:28.990 The letters can be specified either way around. 10 00:00:28.990 --> 00:00:31.950 So the T stands for text mode which is the default. 11 00:00:31.950 --> 00:00:33.820 You don't ask for text or binary. 12 00:00:33.820 --> 00:00:37.250 Now there's an interesting link here that you can go to 13 00:00:37.250 --> 00:00:39.070 to find out more about the file opening mode. 14 00:00:39.070 --> 00:00:40.810 So I'm just going to open that up to show you. 15 00:00:40.810 --> 00:00:42.540 And this link will be in the resources section. 16 00:00:44.440 --> 00:00:49.890 And you can see here the modes on the screen, so we've got various modes there. 17 00:00:49.890 --> 00:00:52.830 And you can see if you don't specify mode, 18 00:00:52.830 --> 00:00:56.950 the default is auto is set to RT to open a file, a text file, for reading. 19 00:00:56.950 --> 00:01:00.060 And as it turns out, we'll be looking at a binary file next in the next video, 20 00:01:00.060 --> 00:01:05.370 but it's also possible to open a file for both reading and writing by using plus. 21 00:01:05.370 --> 00:01:10.940 But that doesn't replace r or w so you have to use r plus or w plus or such. 22 00:01:10.940 --> 00:01:12.290 Now mode x on the screen there, 23 00:01:12.290 --> 00:01:15.960 it can be useful if you expect that file shouldn't already exist and so it will 24 00:01:15.960 --> 00:01:19.920 raise an error if you try to create a file with the same name as the existing one. 25 00:01:19.920 --> 00:01:23.770 And there can be some situations and circumstances where that can be useful. 26 00:01:23.770 --> 00:01:25.940 So before we move on now to binary files and 27 00:01:25.940 --> 00:01:28.710 better ways to store variables, it's time for a challenge. 28 00:01:28.710 --> 00:01:29.750 So let's go back to our code. 29 00:01:29.750 --> 00:01:32.990 And we're gonna create a new file, new path and file, 30 00:01:32.990 --> 00:01:34.600 we're just gonna call it challenge. 31 00:01:34.600 --> 00:01:38.710 So the challenge is going to be as follows. 32 00:01:38.710 --> 00:01:43.160 And what I'm going to do is I'm just going to paste the code in Okay so 33 00:01:43.160 --> 00:01:45.070 there is our challenge. 34 00:01:45.070 --> 00:01:49.460 So we're going to write a program to append a times table to our 35 00:01:50.583 --> 00:01:51.354 in sample dot text. 36 00:01:51.354 --> 00:01:54.020 So we want the tables from two to 12 similar to the output 37 00:01:54.020 --> 00:01:58.800 from the lecture which was in section six of this course. 38 00:01:58.800 --> 00:02:01.700 So the first column of numbers should be right justified. 39 00:02:01.700 --> 00:02:04.370 As an example, the two times table should look like you can see there below. 40 00:02:04.370 --> 00:02:06.520 You can see how that's formatted going from 1 to 12. 41 00:02:06.520 --> 00:02:09.350 So again what you're going to want to do is write a program that's going to 42 00:02:09.350 --> 00:02:13.810 append these tables, these time tables, to the Jabberwocky palm and 43 00:02:13.810 --> 00:02:16.900 sample dot text which you've worked within the last few videos. 44 00:02:16.900 --> 00:02:20.410 So pause the video now, go away, and see if you can come up with that. 45 00:02:20.410 --> 00:02:24.740 Based on what I talked to you about very briefly in this video about a pending. 46 00:02:24.740 --> 00:02:28.600 And you can also refer back to that documentation page that I showed you 47 00:02:28.600 --> 00:02:31.300 to find out the appropriate modes and so forth to use. 48 00:02:31.300 --> 00:02:33.850 So have a go at that and when you're ready to come back and 49 00:02:33.850 --> 00:02:37.510 see a solution or our solution to it come back and I'll show you that. 50 00:02:41.500 --> 00:02:42.420 Okay so did you get on? 51 00:02:42.420 --> 00:02:44.880 Hopefully you managed to get it to work. 52 00:02:44.880 --> 00:02:47.390 So here's the solution now we came up with. 53 00:02:47.390 --> 00:02:51.980 So we're going to start by opening the file so it's with open, 54 00:02:51.980 --> 00:02:56.600 and of course it's going to be sample.txt. 55 00:02:56.600 --> 00:02:58.800 Whoops, I now need to specify the mode, which is a for 56 00:02:58.800 --> 00:03:01.720 pending I'm going to call it tables. 57 00:03:03.290 --> 00:03:07.690 And then I'm going to go 4 I in range, to 215. 58 00:03:07.690 --> 00:03:13.300 And then I'm going to put for J, for J in range 113. 59 00:03:13.300 --> 00:03:21.050 And then I'm going to print using replacement field syntax 60 00:03:25.550 --> 00:03:33.340 Two like so times is two and 61 00:03:33.340 --> 00:03:40.350 we use a .format which is gonna be i j and of course i times j. 62 00:03:40.350 --> 00:03:45.440 And then in the end of it files = tables. 63 00:03:45.440 --> 00:03:48.847 And then we finish it off with a print 64 00:03:52.070 --> 00:03:57.790 times 20, files = tables. 65 00:03:57.790 --> 00:03:59.730 File yet, that should be file. 66 00:04:00.870 --> 00:04:05.110 That should be it, so just confirming that our sample got text at the moment 67 00:04:05.110 --> 00:04:08.920 it's just got the Jabberwocky poem so I'll just close it down again. 68 00:04:08.920 --> 00:04:13.340 And if we now run this we don't get anything output directly 69 00:04:13.340 --> 00:04:16.270 because we've written everything to a file not the console. 70 00:04:16.270 --> 00:04:18.450 But now if I check my sample dot text. 71 00:04:18.450 --> 00:04:24.160 You can see we've got the times tables, correctly showing in the end of that file. 72 00:04:24.160 --> 00:04:26.190 So we've correctly appended to that file. 73 00:04:26.190 --> 00:04:29.430 Now note that each time that we've run this program, I run it again. 74 00:04:29.430 --> 00:04:34.410 And I'll just close it down to open it up, to make it easier. 75 00:04:36.750 --> 00:04:43.020 And, we've got the data repeated twice so its actually come back and done it again. 76 00:04:43.020 --> 00:04:47.090 Because pent output H time we run the program in effect starting at the end of 77 00:04:47.090 --> 00:04:50.020 the file, and if you wanted to create a new file instead, 78 00:04:50.020 --> 00:04:53.730 obviously, head back to our code and change the A to a W. 79 00:04:53.730 --> 00:04:56.520 And, that would create the new file each time. 80 00:04:56.520 --> 00:04:59.790 So, for example, And 81 00:04:59.790 --> 00:05:02.400 I'll just call name something else so we don't overwrite the original file. 82 00:05:02.400 --> 00:05:03.020 And you'll run that. 83 00:05:04.370 --> 00:05:09.410 You'll see sample two appear there on the left hand side. 84 00:05:09.410 --> 00:05:10.720 There's our code. 85 00:05:10.720 --> 00:05:15.720 143 lines there, and if we run it again Just to confirm, 86 00:05:15.720 --> 00:05:19.960 I've still got 143, so it's obviously creating the file each time. 87 00:05:19.960 --> 00:05:23.990 So that's it, that's the end of these three videos on reading and 88 00:05:23.990 --> 00:05:25.420 writing text files. 89 00:05:25.420 --> 00:05:28.200 In the next video, we're going to start looking at binary files, 90 00:05:28.200 --> 00:05:31.180 which is a different mechanism for saving different types of data. 91 00:05:31.180 --> 00:05:32.390 So, see you int hat next video.