WEBVTT 1 00:00:00.260 --> 00:00:01.610 Hi and welcome back. 2 00:00:01.610 --> 00:00:05.557 In this video we're going to talk about copying a file. 3 00:00:05.557 --> 00:00:08.183 And we're gonna do so with a small exercise. 4 00:00:09.030 --> 00:00:11.070 What I've done is still in my project, 5 00:00:11.070 --> 00:00:12.400 just to keep things simple, 6 00:00:12.400 --> 00:00:15.729 I've created a new Python file called Friends.py, 7 00:00:15.729 --> 00:00:17.199 and throughout this project 8 00:00:17.199 --> 00:00:19.490 we're going to be running this file. 9 00:00:19.490 --> 00:00:21.040 I've also created two text files, 10 00:00:21.040 --> 00:00:25.113 nearby_friends.txt, and people.txt. 11 00:00:26.166 --> 00:00:30.283 Inside people.txt you can write a bunch of names. 12 00:00:32.720 --> 00:00:36.370 I'm going to go with the staples of Teclado, 13 00:00:40.534 --> 00:00:41.730 starting with Rolf of course, 14 00:00:41.730 --> 00:00:44.330 and there gonna some Charlies in there. 15 00:00:44.330 --> 00:00:46.449 And these people are just people that are 16 00:00:46.449 --> 00:00:49.773 somewhere near you let's say. 17 00:00:51.360 --> 00:00:55.168 Inside our Friends.py file now we're going to 18 00:00:55.168 --> 00:00:58.080 write a short programme that is going to first 19 00:00:58.080 --> 00:01:01.853 ask the user for a list of three friends. 20 00:01:03.390 --> 00:01:07.410 Then for each friend we're gonna tell the user 21 00:01:07.410 --> 00:01:09.063 whether they're in the same city. 22 00:01:14.150 --> 00:01:17.400 And finally for each friend that is in the same city, 23 00:01:17.400 --> 00:01:20.663 we're going to save them to the nearby friends file. 24 00:01:26.260 --> 00:01:27.300 So it has to do three things. 25 00:01:27.300 --> 00:01:29.460 First ask the user for three friends. 26 00:01:29.460 --> 00:01:33.516 You can do this either using the split method of string, 27 00:01:33.516 --> 00:01:36.470 or you can ask for three distinct friends, 28 00:01:36.470 --> 00:01:38.550 that's fine, three inputs. 29 00:01:38.550 --> 00:01:40.818 Then you'll have to open the people.txt file 30 00:01:40.818 --> 00:01:44.130 and read all of the lines. 31 00:01:44.130 --> 00:01:46.770 You may hint, readlines. 32 00:01:48.680 --> 00:01:52.610 Readlines could be interesting for this exercise. 33 00:01:52.610 --> 00:01:53.880 I recommend that you Google it 34 00:01:53.880 --> 00:01:55.380 and see what you can find. 35 00:01:55.380 --> 00:01:57.200 But of course we're going to implement this 36 00:01:57.200 --> 00:01:58.800 in the project in just a moment. 37 00:01:59.700 --> 00:02:03.010 So you want to read all of the lines in people, 38 00:02:03.010 --> 00:02:05.760 and that's going to give you a list of strings, 39 00:02:05.760 --> 00:02:07.820 a string per line, that's the friends, 40 00:02:07.820 --> 00:02:10.390 or rather that's the people nearby. 41 00:02:10.390 --> 00:02:13.600 Then we're going to compare the people in that file 42 00:02:13.600 --> 00:02:16.310 with the friends that the user entered. 43 00:02:16.310 --> 00:02:18.970 If the friend is amongst those people 44 00:02:18.970 --> 00:02:20.370 we're going to tell the user 45 00:02:20.370 --> 00:02:22.550 and also put them into this file. 46 00:02:22.550 --> 00:02:25.460 So were effectively copying the people file 47 00:02:25.460 --> 00:02:28.320 into the nearby friends file, selectively, 48 00:02:28.320 --> 00:02:31.320 allowing the user to choose which friends they want to copy. 49 00:02:32.900 --> 00:02:36.350 So I'd advise now, I know that this is 50 00:02:36.350 --> 00:02:39.280 a larger undertaking that we've done previously, 51 00:02:39.280 --> 00:02:40.900 and I don't necessarily expect you 52 00:02:40.900 --> 00:02:43.440 to do this right off the bat. 53 00:02:43.440 --> 00:02:47.100 However, by building projects and by struggling a little bit 54 00:02:47.100 --> 00:02:50.930 that is the best way to learn and to really get 55 00:02:50.930 --> 00:02:53.300 familiar with the research that you have to do 56 00:02:53.300 --> 00:02:55.840 in order to become proficient at programming. 57 00:02:55.840 --> 00:02:58.119 So I'd encourage you to pause the video, 58 00:02:58.119 --> 00:03:01.740 give it a go for maybe 20 to 30 minutes, 59 00:03:01.740 --> 00:03:02.760 and then come back 60 00:03:02.760 --> 00:03:05.880 and we will continue implementing this project here, 61 00:03:05.880 --> 00:03:08.330 and I'll explain all the choices that I make 62 00:03:08.330 --> 00:03:10.080 in order to get it to work. 63 00:03:10.080 --> 00:03:10.913 Let's go for it. 64 00:03:12.380 --> 00:03:14.960 Okay I'm sure that you at least 65 00:03:14.960 --> 00:03:17.820 got someplace with this project. 66 00:03:17.820 --> 00:03:21.230 I am confident that you've managed to do 67 00:03:21.230 --> 00:03:23.430 some of the things here, if not all of them. 68 00:03:24.340 --> 00:03:26.390 Now if you do have any trouble, 69 00:03:26.390 --> 00:03:28.360 and things that you're not really sure of 70 00:03:28.360 --> 00:03:30.200 feel free to ask in the course Q & A 71 00:03:30.200 --> 00:03:33.498 before watching this, and I'll be glad to help you 72 00:03:33.498 --> 00:03:35.600 in order to solve any doubts you have 73 00:03:35.600 --> 00:03:38.870 as your own research. 74 00:03:38.870 --> 00:03:41.100 But once you're ready we can continue 75 00:03:41.100 --> 00:03:43.280 and complete this project here. 76 00:03:43.280 --> 00:03:45.100 The first thing to do is to ask the user 77 00:03:45.100 --> 00:03:46.723 for a list of three friends. 78 00:03:47.670 --> 00:03:51.080 So for this I'm going to use the input function of course. 79 00:03:51.080 --> 00:03:53.820 Send I'm going to say enter three friends names 80 00:03:53.820 --> 00:03:58.820 separated by commas, no spaces please. 81 00:03:59.260 --> 00:04:01.340 Now these friends names are going to be 82 00:04:01.340 --> 00:04:04.410 something like Rolf, Jose, Charlie, 83 00:04:04.410 --> 00:04:07.253 something like this is what the user is gonna enter. 84 00:04:07.253 --> 00:04:10.430 So this is going to be a string 85 00:04:10.430 --> 00:04:13.320 that has three friend names separated by commas. 86 00:04:13.320 --> 00:04:17.470 And whenever you have a string that has a separator in it, 87 00:04:17.470 --> 00:04:20.633 like the comma in this case, you can do .split. 88 00:04:21.499 --> 00:04:26.280 And what this does is instead of a string 89 00:04:26.280 --> 00:04:28.030 with two commas and three names, 90 00:04:28.030 --> 00:04:30.763 it's going to give you something that looks like this. 91 00:04:32.700 --> 00:04:34.930 That's a list of three elements. 92 00:04:34.930 --> 00:04:37.238 Notice how the commas are now gone, 93 00:04:37.238 --> 00:04:38.090 they are part of the list, 94 00:04:38.090 --> 00:04:41.190 and instead the elements are made up 95 00:04:41.190 --> 00:04:43.513 into a list of individual names. 96 00:04:45.140 --> 00:04:46.710 That's what the split method does, 97 00:04:46.710 --> 00:04:48.567 I'm not sure if we've looked at that one before. 98 00:04:48.567 --> 00:04:51.260 And nonetheless, that's how you can split a string 99 00:04:51.260 --> 00:04:54.280 into multiple constituent parts. 100 00:04:54.280 --> 00:04:57.080 Of course if you had asked friend one 101 00:04:57.080 --> 00:04:59.950 and said input enter your first friend, 102 00:04:59.950 --> 00:05:02.200 that's totally cool. 103 00:05:02.200 --> 00:05:04.120 That is the way that I would expect you to do it 104 00:05:04.120 --> 00:05:07.313 if you didn't know about this split method, and that's okay. 105 00:05:09.230 --> 00:05:11.310 Now that we've got a list of friends, 106 00:05:11.310 --> 00:05:13.700 of course if you did ask for friend one, 107 00:05:13.700 --> 00:05:15.870 friend two, friend three, then I would expect you 108 00:05:15.870 --> 00:05:18.450 at some point to have created a list 109 00:05:18.450 --> 00:05:22.050 of friend one, friend two, and friend three. 110 00:05:22.050 --> 00:05:23.840 That way you would have a list of friends, 111 00:05:23.840 --> 00:05:26.200 as opposed to three independent variables. 112 00:05:26.200 --> 00:05:31.033 This would be a great way to do it without the split method. 113 00:05:33.396 --> 00:05:34.860 Now that we've got a list of friends though, 114 00:05:34.860 --> 00:05:36.330 we can open people.txt 115 00:05:36.330 --> 00:05:40.170 and read it so that we know the people that are nearby. 116 00:05:40.170 --> 00:05:43.180 So again we're gonna open people.txt 117 00:05:43.180 --> 00:05:46.430 in read mode of course, because we are just reading it, 118 00:05:46.430 --> 00:05:47.790 we're not gonna write to this file, 119 00:05:47.790 --> 00:05:50.050 we're gonna write to the other file. 120 00:05:50.050 --> 00:05:51.610 And then we're gonna look for people 121 00:05:51.610 --> 00:05:53.920 who are in the same city or people that are nearby. 122 00:05:53.920 --> 00:05:57.867 People that are nearby is gonna be file or people.readlines. 123 00:06:02.503 --> 00:06:06.613 The readlines method that I suggested as a hint earlier on 124 00:06:06.613 --> 00:06:08.900 isn't just going to read the file, 125 00:06:08.900 --> 00:06:12.080 but giving you a list of line one, 126 00:06:12.080 --> 00:06:15.190 line two, line three, line four, 127 00:06:15.190 --> 00:06:18.150 which is a pretty handy way of reading a file. 128 00:06:18.150 --> 00:06:21.327 Especially because we've got one friend per line 129 00:06:22.660 --> 00:06:24.260 in our data file. 130 00:06:24.260 --> 00:06:26.370 So that's a good way to go about it. 131 00:06:26.370 --> 00:06:27.260 And if through your research 132 00:06:27.260 --> 00:06:31.090 you didn't quite find this method, that's totally fine, 133 00:06:31.090 --> 00:06:34.370 I'm sure at that point if you couldn't find that method 134 00:06:34.370 --> 00:06:35.890 that was the point where you thought, 135 00:06:35.890 --> 00:06:37.970 you know, I need to keep watching this. 136 00:06:37.970 --> 00:06:40.611 And that's totally fine, of course I don't expect you 137 00:06:40.611 --> 00:06:42.630 to know everything about files 138 00:06:42.630 --> 00:06:44.820 when we haven't even covered it yet. 139 00:06:44.820 --> 00:06:47.747 But nonetheless it's always good to do some research. 140 00:06:47.747 --> 00:06:49.640 Even if you don't find anything, 141 00:06:49.640 --> 00:06:51.490 it's good to train yourself to do that. 142 00:06:51.490 --> 00:06:53.840 And at the end of course we're going to close the file. 143 00:06:53.840 --> 00:06:54.940 Now that we've read it 144 00:06:55.877 --> 00:06:57.550 and we've got our contents in this variable, 145 00:06:57.550 --> 00:06:58.590 we no longer need the file for anything, 146 00:06:58.590 --> 00:07:01.033 we can still access the file with the values. 147 00:07:04.672 --> 00:07:07.180 Next, we're going to find out which people 148 00:07:07.180 --> 00:07:10.373 are friends who are also nearby. 149 00:07:11.370 --> 00:07:16.370 So the best way to do that would be with a set. 150 00:07:16.579 --> 00:07:19.010 And so we've looked at sets before. 151 00:07:19.010 --> 00:07:21.948 What I'll do now is say something like friends set 152 00:07:21.948 --> 00:07:24.023 is a set of friends. 153 00:07:25.470 --> 00:07:27.190 This turns the friends list up here 154 00:07:27.190 --> 00:07:29.510 into a set removing duplicates, 155 00:07:29.510 --> 00:07:32.210 and also making it on order, 156 00:07:32.210 --> 00:07:34.360 and allows us to do things like intersection 157 00:07:34.360 --> 00:07:36.103 which are going to be pretty handy. 158 00:07:36.970 --> 00:07:40.373 Then we'd turn the people nearby into a set. 159 00:07:45.440 --> 00:07:47.520 So now we have two sets, one of friends, 160 00:07:47.520 --> 00:07:48.463 and one of people nearby. 161 00:07:48.463 --> 00:07:51.500 And now we know of course to get the ones that match, 162 00:07:51.500 --> 00:07:54.500 the friends that are nearby, we just need to intersect them. 163 00:07:58.160 --> 00:08:00.475 So we'll do friends_set.intersection, 164 00:08:00.475 --> 00:08:02.133 people nearby set. 165 00:08:03.350 --> 00:08:05.400 And that gives you the friends that are nearby, 166 00:08:05.400 --> 00:08:07.200 because it gives you the elements that match 167 00:08:07.200 --> 00:08:09.943 on both friends set and people nearby set. 168 00:08:12.440 --> 00:08:14.490 Now that we've got our friends that are nearby 169 00:08:14.490 --> 00:08:17.745 we can open the open the nearby friends.txt file, 170 00:08:17.745 --> 00:08:20.384 so let's say nearby friends file 171 00:08:20.384 --> 00:08:25.384 is gonna be open nearby_friends.txt. 172 00:08:26.700 --> 00:08:28.440 And this time it's going to be in write mode 173 00:08:28.440 --> 00:08:30.985 because we're going to be writing some data to it. 174 00:08:30.985 --> 00:08:33.772 And then we can iterate over our nearby friends 175 00:08:33.772 --> 00:08:38.772 for friends nearby set. 176 00:08:41.490 --> 00:08:42.930 I'm gonna call it friends nearby set, 177 00:08:42.930 --> 00:08:46.613 just for consistency since these are all called set. 178 00:08:48.700 --> 00:08:50.640 And we're just going to say hey, 179 00:08:50.640 --> 00:08:51.740 this friend is nearby, 180 00:08:53.770 --> 00:08:56.090 I'm gonna say friend is nearby! 181 00:08:56.090 --> 00:08:58.193 Meet up with them. 182 00:08:59.120 --> 00:09:01.430 And we're also going to save them to the file. 183 00:09:01.430 --> 00:09:05.130 So that would be nearby_friends_file.write, 184 00:09:05.130 --> 00:09:06.773 and then friend. 185 00:09:07.950 --> 00:09:10.270 Now there are a couple of problems 186 00:09:10.270 --> 00:09:12.220 with this code here. 187 00:09:12.220 --> 00:09:14.030 So the first thing we have to do of course, 188 00:09:14.030 --> 00:09:15.764 before we run it, is, 189 00:09:15.764 --> 00:09:19.060 sorry I just pressed the Siri button there by accident, 190 00:09:19.060 --> 00:09:21.570 there's a touch screen, this touch bar thing, 191 00:09:21.570 --> 00:09:24.743 we have to close the file. 192 00:09:26.480 --> 00:09:27.740 Of course, remember to close it 193 00:09:27.740 --> 00:09:30.820 otherwise it will keep open between executions, 194 00:09:30.820 --> 00:09:34.060 even after you've closed the programme, not a good thing. 195 00:09:34.060 --> 00:09:35.443 Okay, so let's run it. 196 00:09:37.000 --> 00:09:39.680 And we can see that it asks us to enter three friend names, 197 00:09:39.680 --> 00:09:42.160 separated by commas, so we can enter 198 00:09:42.160 --> 00:09:44.980 Rolf, Charlie, and Rudolf for example. 199 00:09:44.980 --> 00:09:47.793 That one doesn't exist, but we should get two, right. 200 00:09:49.834 --> 00:09:53.143 Hmm, we get nothing. 201 00:09:54.460 --> 00:09:56.260 Let's check the nearby friends file. 202 00:09:58.160 --> 00:09:59.010 It's got nothing. 203 00:10:01.440 --> 00:10:02.923 So I wonder why that is? 204 00:10:04.390 --> 00:10:08.090 Well, what happens when we encounter a bug? 205 00:10:08.090 --> 00:10:11.670 Of course we must look at the checklist 206 00:10:11.670 --> 00:10:14.020 that I provided earlier, in the last section, 207 00:10:14.020 --> 00:10:15.760 of looking at our code first of all. 208 00:10:15.760 --> 00:10:17.850 So I'm gonna minimise this thing here. 209 00:10:17.850 --> 00:10:20.080 We're gonna once again go through this code 210 00:10:20.080 --> 00:10:21.080 and what it's doing. 211 00:10:23.040 --> 00:10:24.950 We asked the user for some input, 212 00:10:24.950 --> 00:10:27.110 that gets split on the comma. 213 00:10:27.110 --> 00:10:29.060 Now I have got a bit of experience 214 00:10:29.060 --> 00:10:32.620 and I know that this split seems reasonable. 215 00:10:32.620 --> 00:10:35.350 And I know that this is going to provide a list. 216 00:10:35.350 --> 00:10:37.750 So, okay, this looks good so far. 217 00:10:37.750 --> 00:10:39.210 We're opening a file for reading. 218 00:10:39.210 --> 00:10:41.220 And we did this earlier on and it worked fine, 219 00:10:41.220 --> 00:10:44.500 used the same syntax, so this is okay. 220 00:10:44.500 --> 00:10:46.404 And then we're reading the lines. 221 00:10:46.404 --> 00:10:48.030 We didn't readlines earlier on, 222 00:10:48.030 --> 00:10:51.263 so we're not really sure what this is doing. 223 00:10:52.490 --> 00:10:55.200 Then we're closing the file, this seems legitimate. 224 00:10:55.200 --> 00:10:57.540 Then we've got a set of our friends, 225 00:10:57.540 --> 00:10:59.550 and we've looked at sets earlier on, 226 00:10:59.550 --> 00:11:03.420 but I'm not sure if we actually converted a list to a set. 227 00:11:03.420 --> 00:11:04.900 So maybe this is something that we have 228 00:11:04.900 --> 00:11:07.790 to look into before we continue. 229 00:11:07.790 --> 00:11:10.270 As you can see there's a couple of unknowns in this programme. 230 00:11:10.270 --> 00:11:13.208 Like what exactly is readlines doing? 231 00:11:13.208 --> 00:11:16.280 What exactly is set doing? 232 00:11:16.280 --> 00:11:18.350 And we're calling it again, 233 00:11:18.350 --> 00:11:20.260 now I'm telling you these are giving you sets, 234 00:11:20.260 --> 00:11:22.320 and this is the intersection of the two, 235 00:11:22.320 --> 00:11:24.820 so these should be the friends that match. 236 00:11:24.820 --> 00:11:27.260 But then what we're doing is opening the file for writing 237 00:11:27.260 --> 00:11:29.960 and iterating over the friends, and picking them out. 238 00:11:29.960 --> 00:11:32.660 So there's really nothing that could go wrong in here. 239 00:11:33.570 --> 00:11:36.339 The only things that could go wrong is either readlines, 240 00:11:36.339 --> 00:11:38.430 which we are not really sure what it's doing. 241 00:11:38.430 --> 00:11:41.750 We've used it and I've told you what it does, 242 00:11:41.750 --> 00:11:43.690 but maybe there's some small nuance there 243 00:11:43.690 --> 00:11:45.100 that we're not sure about. 244 00:11:45.100 --> 00:11:46.433 And of course the set. 245 00:11:47.720 --> 00:11:49.520 So we didn't get an error, 246 00:11:49.520 --> 00:11:51.430 the checklist for error handling said 247 00:11:51.430 --> 00:11:52.560 that you should copy the error 248 00:11:52.560 --> 00:11:54.240 and go into Google and paste it in. 249 00:11:54.240 --> 00:11:55.340 There's no error here. 250 00:11:56.233 --> 00:11:58.760 So maybe we don't want to go to Google just yet. 251 00:11:58.760 --> 00:12:01.640 We could go through our programme as the computer 252 00:12:01.640 --> 00:12:03.660 checking exactly what we've entered, 253 00:12:03.660 --> 00:12:07.500 exactly what it turns out, and exactly what everything does, 254 00:12:07.500 --> 00:12:09.170 but it's more difficult to do that 255 00:12:09.170 --> 00:12:11.312 because we're not 100% confident on everything 256 00:12:11.312 --> 00:12:15.010 that every single line of code is doing. 257 00:12:15.010 --> 00:12:16.890 And if you're ever at that stage 258 00:12:16.890 --> 00:12:19.100 where you're not 100% confident 259 00:12:19.100 --> 00:12:21.883 that something is doing what you expect, 260 00:12:22.960 --> 00:12:25.164 you have to really be honest with yourself and say, 261 00:12:25.164 --> 00:12:27.763 you know I've never actually ran this before, 262 00:12:28.950 --> 00:12:31.130 maybe it's doing something unexpected. 263 00:12:31.130 --> 00:12:33.670 And I've from this before so I know exactly 264 00:12:33.670 --> 00:12:35.820 what the problem is, (chuckles) but you don't know this. 265 00:12:35.820 --> 00:12:39.500 So the first thing to do is to set a breakpoint 266 00:12:39.500 --> 00:12:42.230 where you are least comfortable, 267 00:12:42.230 --> 00:12:47.230 or rather where the discomfort begins. 268 00:12:47.240 --> 00:12:48.650 So set a breakpoint, 269 00:12:48.650 --> 00:12:51.533 and then just run the programme using the debugger. 270 00:12:52.440 --> 00:12:54.370 Debugger is extremely useful. 271 00:12:54.370 --> 00:12:56.600 So as you can see the first thing that we get is a console, 272 00:12:56.600 --> 00:12:59.580 and it asks us to enter three friends names. 273 00:12:59.580 --> 00:13:01.180 So let's enter the same thing as before 274 00:13:01.180 --> 00:13:03.313 Rolf, Charlie, Rudolph. 275 00:13:06.020 --> 00:13:09.320 So we have our friends list. 276 00:13:09.320 --> 00:13:12.080 As you can see down here it's a list of three elements. 277 00:13:12.080 --> 00:13:14.650 So we're pretty confident on the split method, 278 00:13:14.650 --> 00:13:17.290 and indeed it seems to be giving us what we wanted, 279 00:13:17.290 --> 00:13:19.320 a list of three elements Rolf, 280 00:13:19.320 --> 00:13:20.920 Charlie, and Rudolph down there. 281 00:13:22.312 --> 00:13:24.780 Then we can open the file and we know what this does, 282 00:13:24.780 --> 00:13:27.240 it just gives us a text IO wrapper, 283 00:13:27.240 --> 00:13:30.349 just is a class that allows us to interact with the file. 284 00:13:30.349 --> 00:13:33.697 And then we've got people.readlines. 285 00:13:35.010 --> 00:13:38.323 And so let's press step to the next line. 286 00:13:39.630 --> 00:13:41.100 And then notice that help people nearby 287 00:13:41.100 --> 00:13:43.876 has a quantitive value after we've read the lines, 288 00:13:43.876 --> 00:13:46.603 and the value is a rather bizarre value. 289 00:13:47.560 --> 00:13:51.183 We've got Rolf\n, Jose\n, 290 00:13:52.490 --> 00:13:56.170 Chris\n and so forth until Steve 291 00:13:56.170 --> 00:13:58.053 which does not have a backslash n. 292 00:13:58.970 --> 00:14:01.720 Now I mentioned earlier on, I believe, 293 00:14:01.720 --> 00:14:05.030 what the backslash n means while we were working 294 00:14:05.030 --> 00:14:07.160 on the menu for the milestone project one, 295 00:14:07.160 --> 00:14:08.633 and it's a new line. 296 00:14:09.580 --> 00:14:11.670 So if we opened up people.txt, 297 00:14:11.670 --> 00:14:15.700 you can see that each line has something at the end 298 00:14:15.700 --> 00:14:18.720 that tells the editor to go on to the next line. 299 00:14:18.720 --> 00:14:22.240 Otherwise everything would just continue on the same line. 300 00:14:22.240 --> 00:14:25.230 And that character is the backslash n character, 301 00:14:25.230 --> 00:14:26.940 it means new line. 302 00:14:26.940 --> 00:14:28.080 So we go onto the next line, 303 00:14:28.080 --> 00:14:31.040 and then new line on the end next line, and so forth. 304 00:14:31.040 --> 00:14:34.200 You cannot see that character but it exists, 305 00:14:34.200 --> 00:14:36.340 it is there, that is how the text file knows 306 00:14:36.340 --> 00:14:39.350 to split this into lines, it's not magic. 307 00:14:39.350 --> 00:14:41.870 There is something there telling it go to the next line 308 00:14:41.870 --> 00:14:43.863 and that's this backslash n character. 309 00:14:45.560 --> 00:14:49.130 Okay so now we probably know what the problem is, 310 00:14:49.130 --> 00:14:51.030 but let's continue into the sets 311 00:14:51.030 --> 00:14:52.860 just to analyse exactly what's going on. 312 00:14:52.860 --> 00:14:55.300 We've got a set of three elements, 313 00:14:55.300 --> 00:14:56.610 notice that they are not in the same order, 314 00:14:56.610 --> 00:14:58.950 or in the same order they were before. 315 00:14:58.950 --> 00:15:01.780 We have a set of the people nearby. 316 00:15:01.780 --> 00:15:04.770 And notice how we do have the sets converted here, 317 00:15:04.770 --> 00:15:08.280 but again each element has this backslash n. 318 00:15:08.280 --> 00:15:13.280 So of course when we do the intersection, it's an empty set. 319 00:15:13.970 --> 00:15:18.160 Because Charlie is not equal to Charlie\n, 320 00:15:18.160 --> 00:15:22.180 and Rolf is not equal to Rolf\n. 321 00:15:22.180 --> 00:15:23.630 So this is not going to work. 322 00:15:24.690 --> 00:15:27.720 And at the end of course we iterate over an empty set, 323 00:15:27.720 --> 00:15:29.833 nothing happens and we close the file. 324 00:15:32.310 --> 00:15:33.653 So what can we do? 325 00:15:35.340 --> 00:15:37.740 Well the first thing that I would do 326 00:15:37.740 --> 00:15:40.760 is a list comprehension, and in values comprehension 327 00:15:40.760 --> 00:15:43.920 I would turn the lines into lines 328 00:15:43.920 --> 00:15:46.890 with no backslash n at the end. 329 00:15:46.890 --> 00:15:47.740 So let's do that. 330 00:15:49.310 --> 00:15:51.635 List comprehension, we're going to say line, 331 00:15:51.635 --> 00:15:56.173 strip for line in people.readlines. 332 00:15:57.720 --> 00:15:59.600 Then I'm to set the breakpoint over here 333 00:15:59.600 --> 00:16:01.150 and we're going to debug again. 334 00:16:01.990 --> 00:16:04.083 Enter our three names. 335 00:16:08.668 --> 00:16:10.140 And now notice how the people nearby 336 00:16:10.140 --> 00:16:12.940 no longer have the backslash n. 337 00:16:12.940 --> 00:16:15.890 This is what the strip method does. 338 00:16:15.890 --> 00:16:20.100 Any whitespace like normal spaces, 339 00:16:20.100 --> 00:16:22.580 tabs, or new line characters, 340 00:16:22.580 --> 00:16:27.580 get removed from the start and the end of the string. 341 00:16:27.790 --> 00:16:29.880 So in this case the backslash n's got removed 342 00:16:29.880 --> 00:16:31.920 from the end of the strings, 343 00:16:31.920 --> 00:16:34.843 so that we ended up just with a string with no whitespace. 344 00:16:36.009 --> 00:16:36.900 That's what this strip method does. 345 00:16:36.900 --> 00:16:39.700 Pretty useful, pretty useful method there. 346 00:16:39.700 --> 00:16:42.627 Now we've got the list of people nearby, 347 00:16:42.627 --> 00:16:45.550 these sets are going to match. 348 00:16:45.550 --> 00:16:48.544 You've got Charlie and Charlie, Rudolph and nothing, 349 00:16:48.544 --> 00:16:50.190 and Rolf and Rolf, so let's have a look 350 00:16:50.190 --> 00:16:53.660 see if the friends nearby set does have the values we want. 351 00:16:53.660 --> 00:16:54.980 And indeed it does, 352 00:16:54.980 --> 00:16:57.862 we have Rolf and Charlie our two friends. 353 00:16:57.862 --> 00:16:59.980 And do you notice, the first few times 354 00:16:59.980 --> 00:17:01.720 you run your programmes it may be beneficial 355 00:17:01.720 --> 00:17:03.280 to run through them using the debugger? 356 00:17:03.280 --> 00:17:04.930 Because it just lets you go line by line 357 00:17:04.930 --> 00:17:06.058 examining exactly what's going on. 358 00:17:06.058 --> 00:17:08.760 We've opened the file here, 359 00:17:08.760 --> 00:17:10.120 we're going to iterate over our friends, 360 00:17:10.120 --> 00:17:12.160 the first one we get is Rolf remember, 361 00:17:12.160 --> 00:17:14.410 the set is not ordered so it could be either. 362 00:17:15.570 --> 00:17:16.650 And then we're picking something out, 363 00:17:16.650 --> 00:17:18.400 let's go and check the console. 364 00:17:18.400 --> 00:17:21.430 We can see that world is nearby, we should meet up with him. 365 00:17:21.430 --> 00:17:23.870 And then we're going to write to the friends file. 366 00:17:23.870 --> 00:17:26.900 That has happened but I don't think that the file yet 367 00:17:26.900 --> 00:17:28.400 contains anything, it does not. 368 00:17:28.400 --> 00:17:30.260 Because it won't be saved actually 369 00:17:30.260 --> 00:17:32.353 to the disc until we close the file, 370 00:17:32.353 --> 00:17:35.290 or until we overrun the buffer, 371 00:17:35.290 --> 00:17:37.040 which is not something I want to talk about in this video, 372 00:17:37.040 --> 00:17:37.913 but nonetheless. 373 00:17:38.750 --> 00:17:39.960 Those are the only two moments 374 00:17:39.960 --> 00:17:41.650 when the file actually gets saved to the disc, 375 00:17:41.650 --> 00:17:43.120 it doesn't get saved instantly 376 00:17:43.120 --> 00:17:45.800 in order to improve performance. 377 00:17:45.800 --> 00:17:48.000 So again Charlie, same thing happens, 378 00:17:48.000 --> 00:17:51.170 then we move over to closing the file, and we close it. 379 00:17:51.170 --> 00:17:52.840 We've seen that Rolf is nearby 380 00:17:52.840 --> 00:17:54.468 and Charlie is nearby in the console, 381 00:17:54.468 --> 00:17:57.700 and our file should now have our friends. 382 00:17:57.700 --> 00:18:01.380 But of course we didn't include a backslash n character, 383 00:18:01.380 --> 00:18:03.740 so these friends are all in one line. 384 00:18:03.740 --> 00:18:06.700 So if we go back to our Friends.py 385 00:18:06.700 --> 00:18:10.489 and we put a backslash n after friend, 386 00:18:10.489 --> 00:18:12.370 that means that when we write to the file 387 00:18:12.370 --> 00:18:13.723 we write the friend name, 388 00:18:14.693 --> 00:18:15.526 and then we will also write 389 00:18:15.526 --> 00:18:16.850 this invisible new line character 390 00:18:16.850 --> 00:18:18.467 that will move us into the next line, 391 00:18:18.467 --> 00:18:21.063 and then we will write the next friend there as well. 392 00:18:21.920 --> 00:18:23.840 Also the reason I'm adding a new line at the end 393 00:18:23.840 --> 00:18:25.283 is because PEP 8 asks me to. 394 00:18:26.140 --> 00:18:28.210 It's got this grey squiggly line there. 395 00:18:28.210 --> 00:18:29.543 So yeah. 396 00:18:30.470 --> 00:18:33.220 Okay now we are happy with the way our programme runs, 397 00:18:33.220 --> 00:18:35.240 we can remove the breakpoint 398 00:18:35.240 --> 00:18:37.920 and just play it in normal mode. 399 00:18:37.920 --> 00:18:41.763 Then we can enter three friends. 400 00:18:44.730 --> 00:18:47.070 And you can see that we get Rolf is nearby, 401 00:18:47.070 --> 00:18:48.770 José is nearby, meet up with them. 402 00:18:48.770 --> 00:18:51.490 I thought we had that Anne in people, but I guess it's Anna. 403 00:18:51.490 --> 00:18:54.450 Yep, and then we can go and check our nearby friends, 404 00:18:54.450 --> 00:18:57.850 and we see that they are in now two lines. 405 00:18:57.850 --> 00:19:01.118 Also noticed so that we have line number three, 406 00:19:01.118 --> 00:19:02.960 which is an empty line. 407 00:19:02.960 --> 00:19:05.377 We didn't have that in people. 408 00:19:05.377 --> 00:19:08.030 And to be honest I think that we should have 409 00:19:08.030 --> 00:19:09.230 an empty line at the end, 410 00:19:09.230 --> 00:19:11.220 just so every line is the same. 411 00:19:11.220 --> 00:19:13.810 Every line has the new line character 412 00:19:13.810 --> 00:19:15.215 as opposed to the last line 413 00:19:15.215 --> 00:19:16.048 not having the new line character. 414 00:19:16.048 --> 00:19:16.881 So I'm okay with this. 415 00:19:17.998 --> 00:19:19.000 But of course if you are not, 416 00:19:19.000 --> 00:19:21.480 you can try to experiment with your code. 417 00:19:21.480 --> 00:19:23.590 Make it only print the new line character 418 00:19:23.590 --> 00:19:27.640 when you are not printing the last element of the set. 419 00:19:27.640 --> 00:19:29.930 You can use the innumerate function for that. 420 00:19:29.930 --> 00:19:31.836 I won't go into it in this video, 421 00:19:31.836 --> 00:19:33.466 since we're getting pretty long now. 422 00:19:33.466 --> 00:19:35.730 That's it for this video. 423 00:19:35.730 --> 00:19:39.860 We have created a small app that copies a file selectively. 424 00:19:39.860 --> 00:19:42.984 And we've learned about a bunch of stuff. 425 00:19:42.984 --> 00:19:45.620 First about the new line characters, 426 00:19:45.620 --> 00:19:48.220 they're everywhere and we need to strip them out 427 00:19:48.220 --> 00:19:50.940 if we want to have no whitespace, 428 00:19:50.940 --> 00:19:51.980 that includes spaces, tabs, 429 00:19:51.980 --> 00:19:53.883 and new line characters in a string. 430 00:19:54.920 --> 00:19:57.670 We've learned about the readlines method of the file, 431 00:19:57.670 --> 00:19:59.910 that let's us read each line of the file 432 00:19:59.910 --> 00:20:02.487 as a separate element in a list. 433 00:20:02.487 --> 00:20:04.940 I don't know if we've looked at the split method before, 434 00:20:04.940 --> 00:20:06.730 but if we didn't then we've learned about that. 435 00:20:06.730 --> 00:20:08.520 If we did, we've reviewed it. 436 00:20:08.520 --> 00:20:12.030 It allows us to split a string into constituent parts 437 00:20:12.030 --> 00:20:15.083 according to a particular character in that string. 438 00:20:16.390 --> 00:20:18.220 And then we've also reviewed sets, 439 00:20:18.220 --> 00:20:20.970 and using intersection to calculate matching elements. 440 00:20:20.970 --> 00:20:22.440 Remember I told you that when we looked at sets 441 00:20:22.440 --> 00:20:24.220 that they would be pretty useful? 442 00:20:24.220 --> 00:20:25.990 They are pretty useful. 443 00:20:25.990 --> 00:20:28.550 And finally we also reviewed writing to a file, 444 00:20:28.550 --> 00:20:29.950 something a bit more useful. 445 00:20:31.460 --> 00:20:33.510 So that's it for this video. 446 00:20:33.510 --> 00:20:36.420 Thanks for staying with me and I'll see you on the next one. 447 00:20:36.420 --> 00:20:37.253 Great job today.