WEBVTT 1 00:00:02.240 --> 00:00:05.950 So in this video we're going to do some writing to text files. 2 00:00:05.950 --> 00:00:09.080 So obviously in the previous video we covered reading from them. 3 00:00:09.080 --> 00:00:12.200 So I'm still in the same project that I used for the last video and 4 00:00:12.200 --> 00:00:14.910 we are going to create a new path file. 5 00:00:18.450 --> 00:00:23.140 I'm just going to call this "writing." So the process of 6 00:00:23.140 --> 00:00:27.290 writing data to a text file is very similar to reading from it. 7 00:00:27.290 --> 00:00:31.640 The files opened but this time the mode is w from write instead of r for 8 00:00:31.640 --> 00:00:35.690 read which we saw previously Now the actual data is written using the print 9 00:00:35.690 --> 00:00:39.730 function exactly as you want to display it on the screen but 10 00:00:39.730 --> 00:00:43.860 only this time we specify a file object to send it to and it goes to the file. 11 00:00:43.860 --> 00:00:47.390 And finally it's very important to close the file when you're finished and 12 00:00:47.390 --> 00:00:50.870 once again we can use which we looked at in the previous video so 13 00:00:50.870 --> 00:00:52.290 we don't have to remember to do that ourselves. 14 00:00:52.290 --> 00:00:54.390 So, let's start talking some code. 15 00:00:54.390 --> 00:00:56.830 We're going to start with cities. 16 00:00:56.830 --> 00:00:58.979 So, cities equals LA. 17 00:01:01.446 --> 00:01:03.294 Alice Springs. 18 00:01:05.761 --> 00:01:06.684 Darwin. 19 00:01:09.151 --> 00:01:10.536 Melbourne. 20 00:01:13.003 --> 00:01:14.389 And Sydney. 21 00:01:15.600 --> 00:01:19.015 And we're going to type with open 22 00:01:19.015 --> 00:01:25.630 cities.txt for write, so using a W this time. 23 00:01:26.730 --> 00:01:32.968 S City and then we can type four city in cities, 24 00:01:32.968 --> 00:01:38.630 print city, and here's a different bit now. 25 00:01:38.630 --> 00:01:41.725 File equals City underscore file so 26 00:01:41.725 --> 00:01:46.785 instead of just having it print by itself we're adding the file part of the line 27 00:01:46.785 --> 00:01:50.345 which indicates that we want to write this to a text file. 28 00:01:50.345 --> 00:01:53.225 And again because we're using with here on line three 29 00:01:53.225 --> 00:01:55.535 we don't have to close the file ourself. 30 00:01:55.535 --> 00:01:57.935 So if we go to our folder and we just open that up. 31 00:01:59.690 --> 00:02:03.080 So we're going to go to our folder that contains the code. 32 00:02:03.080 --> 00:02:11.830 It's it's this one here you can see the sample dot txt from the previous video. 33 00:02:11.830 --> 00:02:17.690 So if I did run this and whoops, I run this by default, 34 00:02:17.690 --> 00:02:22.440 which choose the file I I need to actually right-click it and run it the first time. 35 00:02:22.440 --> 00:02:27.500 So nothing appeared in the console output but if we go back to our folder We can see 36 00:02:27.500 --> 00:02:31.370 this now, cities.text has been created and we'll have a look at that, double click. 37 00:02:31.370 --> 00:02:35.530 You see you've got the five cities that I typed originally in 38 00:02:35.530 --> 00:02:38.780 the source code on line one. 39 00:02:38.780 --> 00:02:40.080 So you now know what this parameter was. 40 00:02:40.080 --> 00:02:43.220 I know we talked about that very briefly once in the course. 41 00:02:43.220 --> 00:02:44.930 You can now see how that's used. 42 00:02:44.930 --> 00:02:46.170 Now another thing you might have noticed here, 43 00:02:46.170 --> 00:02:50.390 because we're using the default folder, cities.txt appeared here as well, so 44 00:02:50.390 --> 00:02:53.660 intellijack picked up the fact that there was a file in its folder, and 45 00:02:53.660 --> 00:02:56.840 it enables you to actually view in that as well, which is pretty handy. 46 00:02:56.840 --> 00:03:00.630 And when you specify right mode the file will be, will be created as be solvent 47 00:03:00.630 --> 00:03:04.700 doesn't everything exist, but important if it does exist it will be overwritten. 48 00:03:04.700 --> 00:03:08.130 Now incidentally there's a slight auditiy about the path and convention for 49 00:03:08.130 --> 00:03:10.730 the spaces around operators here. 50 00:03:10.730 --> 00:03:13.380 Now normally you'd put a space either side of the equals symbol and 51 00:03:13.380 --> 00:03:17.900 in a silent operation And it tells you correctly warns you if you forget, but 52 00:03:17.900 --> 00:03:21.360 when using named parameters and function calls, the spaces are frowned upon, 53 00:03:21.360 --> 00:03:24.410 and you get a warning if you put them around the equals here. 54 00:03:24.410 --> 00:03:27.050 In fact, equals here is not actually the assignment operation. 55 00:03:27.050 --> 00:03:31.090 But it's used to provide a named argument in place of the file parameter. 56 00:03:31.090 --> 00:03:33.040 So in other words if I do something here. 57 00:03:33.040 --> 00:03:34.580 We got a warning here. 58 00:03:34.580 --> 00:03:35.210 Now in line. 59 00:03:35.210 --> 00:03:36.110 That's fine. 60 00:03:36.110 --> 00:03:39.430 Put that there to get rid of that. 61 00:03:39.430 --> 00:03:43.790 But if we put a space here and here we'll actually get 62 00:03:43.790 --> 00:03:48.600 a realwaring unexpected spaces around key word slash parameters values. 63 00:03:48.600 --> 00:03:52.964 So On the we need to not have spaces to avoid that 64 00:03:52.964 --> 00:03:55.060 error which I know is a little bit weird. 65 00:03:55.060 --> 00:03:59.310 So just to confirm the equals here in this particular case is normally used for 66 00:03:59.310 --> 00:04:02.410 the and we put eye equals five but there. 67 00:04:02.410 --> 00:04:06.440 But here its not actually in the operation but 68 00:04:06.440 --> 00:04:10.010 it's used to provide a name document In place of the file parameter. 69 00:04:10.010 --> 00:04:14.350 So in other words, city_file is the parameter, it's a named argument. 70 00:04:14.350 --> 00:04:17.430 Now we saw another named argument earlier in the print statement when we passed and 71 00:04:17.430 --> 00:04:20.270 equals in two single quotes, and we'll be looking at functions and 72 00:04:20.270 --> 00:04:22.340 parameters in more detail later in the course. 73 00:04:22.340 --> 00:04:26.650 So just for now, humor IntelliJ, remove the spaces around the equal 74 00:04:26.650 --> 00:04:30.400 When specifying named arguments like we've done in line five. 75 00:04:30.400 --> 00:04:35.960 Now before we leave named arguments print argument that can be set to true or false. 76 00:04:35.960 --> 00:04:38.920 And the false is false so data isn't flushed. 77 00:04:38.920 --> 00:04:40.180 Now to understand what this is for 78 00:04:40.180 --> 00:04:43.450 we have to look a little bit in more detail about what is happening. 79 00:04:43.450 --> 00:04:45.810 When Python writes data to a file. 80 00:04:45.810 --> 00:04:49.630 Now, external devices on your computer including the screen 81 00:04:49.630 --> 00:04:51.950 are much slower than computer memory. 82 00:04:51.950 --> 00:04:55.590 So, running with things like disc drives in the screen is buffered. 83 00:04:55.590 --> 00:04:59.330 So what that means is that the data is actually written to a buffer, and 84 00:04:59.330 --> 00:05:03.090 the contents of that buffer is transferred to the external device in the background. 85 00:05:03.090 --> 00:05:07.460 So it's something that happens, While you're sort of doing something else. 86 00:05:07.460 --> 00:05:11.420 This allows the program to continue processing without waiting for 87 00:05:11.420 --> 00:05:12.760 the write to complete. 88 00:05:12.760 --> 00:05:14.220 And so, everything seems much faster. 89 00:05:14.220 --> 00:05:19.160 So in other words, as soon as we run this program, Once the process finished, 90 00:05:19.160 --> 00:05:22.550 at that point it may still be writing something in the background to that file. 91 00:05:22.550 --> 00:05:24.050 Now this, obviously an example, 92 00:05:24.050 --> 00:05:27.030 you have five words that would have been pretty quick. 93 00:05:27.030 --> 00:05:29.820 But you can imagine like a huge file writing that was perhaps many 94 00:05:29.820 --> 00:05:30.950 megabytes long. 95 00:05:30.950 --> 00:05:33.260 It does take the computer some time to process. 96 00:05:33.260 --> 00:05:37.450 And that's because it is putting in that buffer It all then 97 00:05:37.450 --> 00:05:41.230 comes down to the speed of the device that you're writing the information to.So 98 00:05:41.230 --> 00:05:44.660 essentially it means that the data is actually written to the buffer and then 99 00:05:44.660 --> 00:05:48.100 the contents of the buffer is transferred to the external device in the background. 100 00:05:48.100 --> 00:05:49.180 And again, we're doing that or 101 00:05:49.180 --> 00:05:51.770 it happens automatically to allow the program to continue. 102 00:05:51.770 --> 00:05:54.990 Without waiting for that write process to complete. 103 00:05:54.990 --> 00:05:58.440 Sometimes though with that said, you want the data to be written out immediately. 104 00:05:58.440 --> 00:06:00.780 So especially if the external device is a screen, and 105 00:06:00.780 --> 00:06:03.220 you want the user to be able to see the output straight away. 106 00:06:03.220 --> 00:06:06.300 So with buffering, data could be sent to the screen from the buffer for 107 00:06:06.300 --> 00:06:10.610 example, and then immediately overwritten by subsequent data from the buffer. 108 00:06:10.610 --> 00:06:13.290 So this wouldn't be apparent in our example so far. 109 00:06:13.290 --> 00:06:16.470 Because it already using relatively small amounts of data but 110 00:06:16.470 --> 00:06:19.311 displaying the last on the screen could suffer from that effect. 111 00:06:19.311 --> 00:06:23.230 As the scrolls up the screen lines of text appear to flicker. 112 00:06:23.230 --> 00:06:27.470 Now, closing a file causes the buffer to be flushed automatically. 113 00:06:27.470 --> 00:06:29.440 But if you wanna ensure your data's written sooner, 114 00:06:29.440 --> 00:06:34.300 you can pass flush equals true to cause the data to be written immediately. 115 00:06:34.300 --> 00:06:37.230 Generally though with that said, it's not something you'd, you really should worry 116 00:06:37.230 --> 00:06:40.930 about, but the ability to control it through a window is important 117 00:06:40.930 --> 00:06:46.290 enough that the benefits Python introduced the flash parameter in Python three. 118 00:06:46.290 --> 00:06:50.190 Remembering that with that said that Python was but Python three was released 119 00:06:50.190 --> 00:06:54.840 in 2008, and although they may not seem very a longer time ago A modern computer 120 00:06:54.840 --> 00:06:59.680 is three times as fast as they were then and interestingly enough a modern 121 00:06:59.680 --> 00:07:04.200 mobile phone more than likely has a faster processor than a 2008 desktop computer. 122 00:07:04.200 --> 00:07:08.470 So things have come a long way and the point we're making here is 123 00:07:08.470 --> 00:07:11.470 you probably generally speaking don't need to use the first parameter. 124 00:07:11.470 --> 00:07:13.690 But it's there if you decide to use it. 125 00:07:13.690 --> 00:07:21.160 So you go about doing something like flash equals true. 126 00:07:21.160 --> 00:07:23.780 That's actually how you do it if you wanted to actually use that flush command. 127 00:07:23.780 --> 00:07:28.330 And generally we don't recommend that you need to do that for 128 00:07:28.330 --> 00:07:30.580 most of the situations. 129 00:07:30.580 --> 00:07:33.700 So okay data from the last video. 130 00:07:33.700 --> 00:07:37.170 And we've now seen how to write our little file worked earlier in this video. 131 00:07:37.170 --> 00:07:40.380 But before we move on it's probably useful to see if we can read back 132 00:07:40.380 --> 00:07:41.580 the data we've just written. 133 00:07:41.580 --> 00:07:45.400 So let's that bit out, because we've already now written the data. 134 00:07:47.510 --> 00:07:49.740 We don't need to call them again because the file already exists so 135 00:07:49.740 --> 00:07:52.410 we're going to start with an empty list. 136 00:07:54.890 --> 00:08:02.601 And with open, with open cities.txt for 137 00:08:02.601 --> 00:08:07.520 read as city_file. 138 00:08:07.520 --> 00:08:10.714 This is our first study, for city in city_file. 139 00:08:12.960 --> 00:08:16.350 You can try something like cities.append(city). 140 00:08:16.350 --> 00:08:23.270 Then we can do a print(cities). 141 00:08:23.270 --> 00:08:29.590 Then maybe, for city in cities and print(city). 142 00:08:29.590 --> 00:08:32.740 So now we're going through opening the file using with. 143 00:08:32.740 --> 00:08:35.440 We're going through each city, we're appending it to our list. 144 00:08:35.440 --> 00:08:39.180 Then we're printing up the list and you're just going through each entry in the list 145 00:08:39.180 --> 00:08:42.410 and printing it again just to confirm that we can save the individual cities. 146 00:08:42.410 --> 00:08:43.290 So if we run this 147 00:08:45.120 --> 00:08:48.480 you can see we've got notice that slash n exists on all of those as well. 148 00:08:48.480 --> 00:08:51.609 And then we can see that the five examples. 149 00:08:51.609 --> 00:08:54.245 So in other words this works after a fashion. 150 00:08:54.245 --> 00:08:55.140 But we have those, 151 00:08:55.140 --> 00:08:59.900 these additional new line characters up here in the list but also after each line. 152 00:08:59.900 --> 00:09:03.730 And one good way that we can remove that is we can use the strip function, 153 00:09:03.730 --> 00:09:05.620 from a string to remove those characters from a string. 154 00:09:05.620 --> 00:09:09.150 Instead of waiting until you print it out, I'm doing something in the print here, 155 00:09:09.150 --> 00:09:13.410 like putting end equals single quote single quote there to remove it. 156 00:09:13.410 --> 00:09:16.990 It can be a good idea to remove it at the time we're importing it. 157 00:09:18.480 --> 00:09:22.594 So we can do something like, append, and we can put .strip 158 00:09:26.372 --> 00:09:28.715 .strip/n. 159 00:09:28.715 --> 00:09:33.570 So if you then run that We see we get a much better result. 160 00:09:33.570 --> 00:09:35.720 You notice the /n doesn't appear in our list anymore. 161 00:09:35.720 --> 00:09:36.540 And obviously, 162 00:09:36.540 --> 00:09:41.210 each entry is outputted without the any line character which is pretty cool. 163 00:09:42.240 --> 00:09:46.050 So, to see how strip's used let's temporarily create a new file. 164 00:09:46.050 --> 00:09:50.950 We're just going to call it scratch pad. 165 00:09:54.730 --> 00:09:56.840 So, if you happen to be using idol you can just type these 166 00:09:56.840 --> 00:09:58.058 commands directly into idol. 167 00:09:58.058 --> 00:09:58.870 Assuming you. 168 00:09:58.870 --> 00:10:03.130 You need to do just what I have done and create a new file. 169 00:10:03.130 --> 00:10:10.200 We do something like print Adelaide.strip A. 170 00:10:10.200 --> 00:10:14.200 Then we run that and 171 00:10:14.200 --> 00:10:17.080 it will say problem with running it again so I'm going to just right click that. 172 00:10:17.080 --> 00:10:18.770 And run scratchpad. 173 00:10:18.770 --> 00:10:22.250 And notice how it's taking the a off, so it's stripped off the a. 174 00:10:22.250 --> 00:10:27.060 So what strip does, it removes characters from the beginning or end of a string and 175 00:10:27.060 --> 00:10:28.570 only from the beginning or end. 176 00:10:28.570 --> 00:10:29.350 That's an important thing. 177 00:10:29.350 --> 00:10:35.170 So for example, if I went back now and changed this To del, 178 00:10:35.170 --> 00:10:37.593 which you can see. 179 00:10:37.593 --> 00:10:38.530 I ran that. 180 00:10:38.530 --> 00:10:40.890 So you can see what's happened there. 181 00:10:40.890 --> 00:10:45.060 The intention here when we ran it was to remove the del, the second, third, and 182 00:10:45.060 --> 00:10:46.583 fourth characters from. 183 00:10:46.583 --> 00:10:49.370 But what actually happened was it ignored those. 184 00:10:49.370 --> 00:10:53.550 But, it did remove the DE on the end, and it ignored the L. 185 00:10:53.550 --> 00:10:57.560 So, very important that's a partial match and its only returning, stripping 186 00:10:57.560 --> 00:11:01.510 anything that's in there even its only a partial match as you can see there. 187 00:11:01.510 --> 00:11:02.810 So, back to our program again. 188 00:11:02.810 --> 00:11:06.680 Back to writing, and I'll just select that before I forget again. 189 00:11:06.680 --> 00:11:10.630 So on line 11, our program now strips slash n, 190 00:11:10.630 --> 00:11:14.230 which is the new line character, before they're appended to the list. 191 00:11:14.230 --> 00:11:18.530 So one thing to note is that Python is aware of which operating system 192 00:11:18.530 --> 00:11:22.230 it's running on, and it handles what is referred to as universal newlines. 193 00:11:22.230 --> 00:11:27.230 Now on UNIX systems, such as Linux and a Mac, New lines are written using a single 194 00:11:27.230 --> 00:11:32.120 character, where as Windows uses two characters which is a /r or a /m. 195 00:11:32.120 --> 00:11:36.280 Sometimes now it's written as CR-LF for carriage return and line feed, back 196 00:11:36.280 --> 00:11:39.850 from the the days when printers needed codes to correctly position the paper. 197 00:11:39.850 --> 00:11:41.540 For the next line of text. 198 00:11:41.540 --> 00:11:44.850 But the good thing is the point we're making here is that Python's aware of this 199 00:11:44.850 --> 00:11:47.490 and automatically takes care of the different conventions for 200 00:11:47.490 --> 00:11:51.000 handling line breaks for you for different operating systems. 201 00:11:51.000 --> 00:11:54.690 But, this is the important thing, as long as the files open in text mode. 202 00:11:54.690 --> 00:11:56.410 Does need to be opened in text mode for that to happen. 203 00:11:56.410 --> 00:11:59.440 So we're going to finish the discussion on text files by observing that 204 00:11:59.440 --> 00:12:03.010 anything that you can print out on screen can be written to a text file. 205 00:12:03.010 --> 00:12:07.240 However, it's not always possible to read it back in the original form. 206 00:12:07.240 --> 00:12:10.360 And to show you that, you're going to run the following program. 207 00:12:10.360 --> 00:12:13.190 And we'll see exactly what we mean. 208 00:12:16.140 --> 00:12:23.975 So down here, I'm gonna type Imelda equals Mayhem. 209 00:12:23.975 --> 00:12:28.720 [NOISE] My 2011. 210 00:12:28.720 --> 00:12:36.125 And it's one, you saw this obviously in the earlier lectures. 211 00:12:36.125 --> 00:12:45.400 [NOISE] 212 00:12:45.400 --> 00:12:46.150 Do a couple more. 213 00:12:56.830 --> 00:12:58.825 Okay so there's the data we want to [INAUDIBLE] so 214 00:12:58.825 --> 00:13:01.202 if we do something like with open. 215 00:13:03.144 --> 00:13:10.985 Three, double and the order as before. 216 00:13:10.985 --> 00:13:15.237 And we do something like print, 217 00:13:16.254 --> 00:13:19.573 file equals file. 218 00:13:20.870 --> 00:13:24.660 So that's obviously gonna run and print out our top. 219 00:13:24.660 --> 00:13:28.990 So if we run that, just for simplicity I'll change that to a text file, .txt. 220 00:13:28.990 --> 00:13:33.990 And if we run that, and there's our .txt. 221 00:13:33.990 --> 00:13:34.700 We'll have a look at that. 222 00:13:34.700 --> 00:13:37.580 And you can see that, just make a bit space here. 223 00:13:39.690 --> 00:13:44.140 So the contents of the file probably look like a good representation of our tuple, 224 00:13:44.140 --> 00:13:48.370 but there's no easy way to read it back into a tuple variable, 225 00:13:48.370 --> 00:13:50.720 because it's now stored as a string. 226 00:13:50.720 --> 00:13:55.150 So Python does have an eval function that will evaluate any expression 227 00:13:55.150 --> 00:13:56.120 to a string passed to it, so 228 00:13:56.120 --> 00:13:59.760 we could get the contents back from the file into a tuple variable by using eval. 229 00:13:59.760 --> 00:14:01.610 So we'll just close it down. 230 00:14:03.050 --> 00:14:05.570 And we'll just comment this out now because we know what 231 00:14:06.690 --> 00:14:07.980 file we're going to read the file again. 232 00:14:07.980 --> 00:14:16.005 So I'm going to put with open, and in order three dot TXT, we'll read this time. 233 00:14:16.005 --> 00:14:20.225 As imelda_file contents 234 00:14:20.225 --> 00:14:25.590 equals imelda_file.readline. 235 00:14:25.590 --> 00:14:33.800 And we can do something like imelda equals eval contents. 236 00:14:37.860 --> 00:14:42.342 We can do print imelda Title, 237 00:14:42.342 --> 00:14:47.450 artist, year, tracks equals imelda. 238 00:14:47.450 --> 00:14:52.320 print title, print artist, print year. 239 00:14:52.320 --> 00:14:57.708 Okay so if you run 240 00:14:57.708 --> 00:15:03.360 that So it does allow us, 241 00:15:03.360 --> 00:15:06.725 in this case, to retrieve the contents of the type of the file. 242 00:15:06.725 --> 00:15:09.570 But there are much better ways to do it as we'll see. 243 00:15:09.570 --> 00:15:12.650 So the use of evals is not a good idea when dealing with data external to your 244 00:15:12.650 --> 00:15:18.140 program, and that's because the contents of the file could be changed, and frankly 245 00:15:18.140 --> 00:15:21.460 could contain damaging instructions and your program would happily execute them. 246 00:15:21.460 --> 00:15:24.060 It's really important to design programs with security in mind and 247 00:15:24.060 --> 00:15:26.790 not to leave vulnerabilities in your code. 248 00:15:26.790 --> 00:15:28.780 So I'm gonna finish the video here, now. 249 00:15:28.780 --> 00:15:31.010 In the next video we're gonna talk about appending to a text file. 250 00:15:31.010 --> 00:15:33.520 And then we're going to come up with a challenge, for 251 00:15:33.520 --> 00:15:37.330 what we've learned to do with reading, writing, and appending text files. 252 00:15:37.330 --> 00:15:38.510 So we'll do that in the next video