WEBVTT 1 00:00:02.230 --> 00:00:05.730 So this section, we're gonna look at input and output in Python. 2 00:00:05.730 --> 00:00:09.160 So, I/O, input output just means getting data into a program and 3 00:00:09.160 --> 00:00:10.420 getting it out again. 4 00:00:10.420 --> 00:00:14.040 Now, we've already seen one way to enter data into our programs from the keyboard. 5 00:00:14.040 --> 00:00:17.380 But in this section we're gonna be looking at another way that can also be useful. 6 00:00:17.380 --> 00:00:20.290 And we've also covered printing results to the screen. 7 00:00:20.290 --> 00:00:24.280 But so far every time we've needed data in our program, we've had to type it in. 8 00:00:24.280 --> 00:00:27.760 So wouldn't it be useful to be able to save that data to a file so 9 00:00:27.760 --> 00:00:29.520 that we can use it over and over again? 10 00:00:29.520 --> 00:00:32.150 So in this section we're gonna be looking at a number of ways to write data to 11 00:00:32.150 --> 00:00:34.770 a file using the computer's disk drive. 12 00:00:34.770 --> 00:00:38.290 And we'll also include writing the data in a similar way, 13 00:00:38.290 --> 00:00:40.090 To how it's printed on the screen. 14 00:00:40.090 --> 00:00:44.760 This is a process you'll understand a little bit later. 15 00:00:44.760 --> 00:00:48.290 Another process we're also going to look at how python dictates object provide 16 00:00:48.290 --> 00:00:50.930 the basis for a very useful way to store data. 17 00:00:50.930 --> 00:00:51.910 So, let's make a start. 18 00:00:51.910 --> 00:00:54.950 The first we're gonna talk about reading text files. 19 00:00:54.950 --> 00:00:58.110 And the good thing about this is that reading text files in Python 20 00:00:58.110 --> 00:01:02.140 is incredibly easy and just really consists of three simple steps. 21 00:01:02.140 --> 00:01:03.520 So, first we have to open the file. 22 00:01:03.520 --> 00:01:07.535 And we And we use that using the Python built in function called Open. 23 00:01:07.535 --> 00:01:10.295 Then the next step is to read in the file which we can do either one line at 24 00:01:10.295 --> 00:01:14.435 a time or we can actually try and read in the entire file in one go. 25 00:01:14.435 --> 00:01:17.675 Finally to close the file when we're done with it is the final step. 26 00:01:17.675 --> 00:01:20.655 Now this is very important, especially when writing to a file 27 00:01:20.655 --> 00:01:23.745 because if it's not done the file can actually get corrupted. 28 00:01:23.745 --> 00:01:26.805 Now the resources section of this video contains a text file that you can see on 29 00:01:26.805 --> 00:01:28.180 the screen there now. 30 00:01:28.180 --> 00:01:30.300 That we can use to practice reading a file. 31 00:01:30.300 --> 00:01:33.630 So download that file, and place it in a suitable directory in your local drive. 32 00:01:33.630 --> 00:01:36.390 It contains Lewis Carroll's poem Jabberwocky, 33 00:01:36.390 --> 00:01:37.820 from through The Looking Glass. 34 00:01:37.820 --> 00:01:39.930 So we're going to use that, and read that into Python. 35 00:01:39.930 --> 00:01:43.620 Now I've actually stored this on my desktop. 36 00:01:43.620 --> 00:01:45.960 But you can literally store anywhere. 37 00:01:45.960 --> 00:01:49.980 So, let's go ahead now and write a program that will read this file into Python. 38 00:01:49.980 --> 00:01:56.135 So, we're gonna start by typing Java = and 39 00:01:56.135 --> 00:02:01.075 We're gonna type open which is the built in function to open a file. 40 00:02:01.075 --> 00:02:04.248 We're gonna put home/dev/Desktop. 41 00:02:04.248 --> 00:02:09.665 Now that's actually the path that I've got on my computer 42 00:02:09.665 --> 00:02:14.155 to this file and it's called sample.txt. 43 00:02:14.155 --> 00:02:18.435 Like so, and then we can go ahead and type for line in jober. 44 00:02:20.075 --> 00:02:27.110 Print line, and lastly, jabber.close. 45 00:02:27.110 --> 00:02:30.280 So again, the first line opens the text file on line one. 46 00:02:30.280 --> 00:02:34.750 On line three and four, we're actually cycling through each line in the document. 47 00:02:34.750 --> 00:02:36.540 Then we're printing it out one line at a time. 48 00:02:36.540 --> 00:02:39.080 So we're cycling right through to the end of the file. 49 00:02:39.080 --> 00:02:41.120 And line six closes at the file when we're done. 50 00:02:41.120 --> 00:02:44.150 Now the other thing I didn't put here which we actually need 51 00:02:44.150 --> 00:02:47.350 at the end of the open is we need to provide the mode as well. 52 00:02:47.350 --> 00:02:50.270 So if you think about it when you're opening a text file or 53 00:02:50.270 --> 00:02:54.690 a file on your computer you need to tell it what type of input output you want. 54 00:02:54.690 --> 00:02:57.980 So in this case we don't want to write to this file, we want to read to it. 55 00:02:57.980 --> 00:03:00.980 So to do that we just Put a semicolon and 56 00:03:00.980 --> 00:03:06.620 we put the letter R in single quotes, which again stands for read-only or read. 57 00:03:06.620 --> 00:03:10.510 Now the thing here also is you can provide a full path, as you can see on the screen 58 00:03:10.510 --> 00:03:13.852 here, or you can top a relative path as part of the [INAUDIBLE]. 59 00:03:13.852 --> 00:03:16.580 This is the first example; this is a full path. 60 00:03:16.580 --> 00:03:20.960 If you're on a Mac or on a Linux system Just run this first just to check that it 61 00:03:20.960 --> 00:03:22.980 works, then we'll talk about the Windows side of things. 62 00:03:25.870 --> 00:03:27.990 And I'm actually getting an error that the file isn't found, so 63 00:03:27.990 --> 00:03:30.590 I'll just confirm that it is on my desktop. 64 00:03:30.590 --> 00:03:34.910 I'm gonna open my terminal We probably made a mistake in the path. 65 00:03:34.910 --> 00:03:35.980 I'm gonna go to my desktop. 66 00:03:37.990 --> 00:03:39.290 That's why I put in /Home. 67 00:03:39.290 --> 00:03:42.040 It should be /Users/dev/Desktop. 68 00:03:42.040 --> 00:03:47.230 So you can see there, that's the path on my computer to the desktop. 69 00:03:47.230 --> 00:03:50.380 So I'm too used to typing /Home being on a Linux system. 70 00:03:50.380 --> 00:03:52.650 So I'm gonna go back and change the code. 71 00:03:52.650 --> 00:03:53.360 And paste that in. 72 00:03:53.360 --> 00:03:58.050 And put another slash there in the name of the file. 73 00:03:58.050 --> 00:04:01.150 We'll try to run that again, and we should be able to get it to work. 74 00:04:01.150 --> 00:04:04.560 You can see that we've got all the data coming from the text file and 75 00:04:04.560 --> 00:04:07.300 the poem's coming out quite nicely, so that's good. 76 00:04:07.300 --> 00:04:11.280 Now the thing to point out here is again this is a mech path which is very similar 77 00:04:11.280 --> 00:04:11.910 to Linux path. 78 00:04:11.910 --> 00:04:13.770 You'll notice the slashes are going forward. 79 00:04:13.770 --> 00:04:16.210 And that's the convention for mac influence. 80 00:04:16.210 --> 00:04:18.910 But if you're running on a Windows computer, and 81 00:04:18.910 --> 00:04:22.340 you had a username of Tim, you'd probably do something like this. 82 00:04:22.340 --> 00:04:24.640 You'd type java, equals open. 83 00:04:24.640 --> 00:04:28.900 And it'd be something like C colon backslash documents. 84 00:04:32.060 --> 00:04:40.535 Is settings/tim/mydocumentssample.text, if you have it in your documents folder, 85 00:04:40.535 --> 00:04:44.790 sample.text, and The origin to set it up for read only mode. 86 00:04:44.790 --> 00:04:47.710 Now, that doesn't always work with the slashes with space. 87 00:04:47.710 --> 00:04:49.000 Sometimes it's bit of an issue. 88 00:04:49.000 --> 00:04:53.560 And there's also certain characters to cause a problem, 89 00:04:53.560 --> 00:04:55.585 because if you put a /n for argument sake. 90 00:04:55.585 --> 00:04:59.630 /n is interpreted as a command and /t as well. 91 00:04:59.630 --> 00:05:02.070 So, what it can be a good idea to do for 92 00:05:02.070 --> 00:05:06.780 your slashes is to put double slashes here and that just makes it clear to Python. 93 00:05:06.780 --> 00:05:11.510 This is a path, and it doesn't try and interpret the character after the slash as 94 00:05:11.510 --> 00:05:13.060 something else, so you might do something like that. 95 00:05:13.060 --> 00:05:15.780 Even though that looks a little bit funny, 96 00:05:15.780 --> 00:05:19.170 that does make sure that that makes--you know, really makes sure that the path is 97 00:05:19.170 --> 00:05:22.120 going to be treated as a path and not as something else. 98 00:05:22.120 --> 00:05:22.900 So I'll comment that out and 99 00:05:22.900 --> 00:05:26.730 go back to my code because obviously I'm on a Mac, so I'll need that to work. 100 00:05:26.730 --> 00:05:28.660 Now if I type in something, well, 101 00:05:28.660 --> 00:05:31.810 we saw the example earlier where the path doesn't exist when we run it. 102 00:05:31.810 --> 00:05:35.540 We get an error to say, cannot find In the file. 103 00:05:35.540 --> 00:05:37.630 So it's pretty clear that there's a problem out in the file, and so 104 00:05:37.630 --> 00:05:39.510 it will actually crash at that point. 105 00:05:39.510 --> 00:05:40.810 And put that back in there. 106 00:05:40.810 --> 00:05:43.390 So again just a note about slashes. 107 00:05:43.390 --> 00:05:46.740 The forward slashes work equally on a Mac and a Linux machine. 108 00:05:46.740 --> 00:05:48.000 But, they're, but the same. 109 00:05:48.000 --> 00:05:50.450 But with a Windows machine it's back slashes. 110 00:05:50.450 --> 00:05:53.030 But you can't use back slashes in a Linux or 111 00:05:53.030 --> 00:05:55.400 Mac path, you need to use the forward slashes. 112 00:05:55.400 --> 00:05:58.140 So You wanna get clear based on your operating system as to 113 00:05:58.140 --> 00:05:59.810 which way you're using your slashes. 114 00:05:59.810 --> 00:06:01.530 Obviously if I haven't got the path in there, 115 00:06:01.530 --> 00:06:06.750 if I actually remove my path altogether, I run that, I'm going to get an error. 116 00:06:06.750 --> 00:06:09.025 And the error is you can say it's looking for this folder, 117 00:06:09.025 --> 00:06:14.360 user/dev/documents-programs/pauseandprogr- ams/folderIO. 118 00:06:14.360 --> 00:06:15.980 It's looking for that folder, And 119 00:06:15.980 --> 00:06:18.740 it's actually looking for sample.text in that folder. 120 00:06:18.740 --> 00:06:22.390 So that's why it's best to have relativepath to get that to work. 121 00:06:22.390 --> 00:06:24.840 If you're not located in that relative path, 122 00:06:24.840 --> 00:06:26.170 it's not going to actually work for you. 123 00:06:27.840 --> 00:06:29.790 What I can do, though, is set up a new. 124 00:06:31.020 --> 00:06:32.030 Go back into our folder. 125 00:06:33.890 --> 00:06:38.554 Now, take a copy of that and what I'm going to do is go into the programs, 126 00:06:38.554 --> 00:06:43.930 Python, File I/O, I'm going to paste the sample of text in there. 127 00:06:43.930 --> 00:06:46.622 So I've now got it in my path, which you can see is 128 00:06:46.622 --> 00:06:50.310 Users\Dev\Documents\Programs\Python Programs\File IO. 129 00:06:50.310 --> 00:06:54.270 So now if I run that, without specifying a path, because we know the path 130 00:06:54.270 --> 00:06:58.480 by looking up here We run that, we find that it does find it. 131 00:06:58.480 --> 00:07:03.170 So it's all a matter of choosing the right folder to put the files in. 132 00:07:03.170 --> 00:07:05.650 You can even just specify a path in full. 133 00:07:05.650 --> 00:07:08.560 Or if you know you're in that particular folder, you can just use the, 134 00:07:08.560 --> 00:07:12.180 or select the actual name, without specifying the path. 135 00:07:12.180 --> 00:07:15.700 so at this point forward, I'm not going to specify a path anymore so 136 00:07:15.700 --> 00:07:19.880 all the files that we read and write are going to be in that particular folder so 137 00:07:19.880 --> 00:07:23.030 keep in mind that when you're working along with this, to make sure you put 138 00:07:23.030 --> 00:07:28.400 the symbol dot text in the actual folder where this particular program resides. 139 00:07:28.400 --> 00:07:30.750 Now, when you go online to find Python examples, or 140 00:07:30.750 --> 00:07:35.320 you read the official documentation, you'll come across the term Pythonic to 141 00:07:35.320 --> 00:07:39.710 describe the Python way of doing things, compared to other programming languages. 142 00:07:39.710 --> 00:07:42.670 And I've used that word a few times as we've been through. 143 00:07:42.670 --> 00:07:47.260 Now, this is an excellent example of what Pythonic means. 144 00:07:47.260 --> 00:07:50.410 In most of the languages, after opening the file, You have to check for 145 00:07:50.410 --> 00:07:52.770 the end of the file, normally by using a while loop. 146 00:07:52.770 --> 00:07:56.050 But Python, that's to interact through the lines of the file 147 00:07:56.050 --> 00:07:58.510 as though it were a list, which is pretty fabulous. 148 00:07:58.510 --> 00:07:59.290 And you can, of course, 149 00:07:59.290 --> 00:08:01.970 perform another processing as you iterate through the files. 150 00:08:01.970 --> 00:08:03.830 For example, we could check each line, and 151 00:08:03.830 --> 00:08:06.930 only print out the ones containing the title character, for arguments' sake. 152 00:08:06.930 --> 00:08:15.720 So, we could do something like this, if Jabberwalk, in, line dot lower. 153 00:08:15.720 --> 00:08:19.340 I'm converting it to lower case there so we can do a case check 154 00:08:19.340 --> 00:08:22.360 against the stream that we've entered, which is already lower case. 155 00:08:22.360 --> 00:08:26.150 Only then, you can get a print line for this, 156 00:08:26.150 --> 00:08:30.950 also print end equals what so, so it doesn't go into the next line. 157 00:08:30.950 --> 00:08:31.450 Run that. 158 00:08:33.320 --> 00:08:36.500 I'm not too sure what's showing an error there, but the code is definitely valid, 159 00:08:36.500 --> 00:08:40.630 so it could be something to do with the new version of the Python plug in perhaps, 160 00:08:40.630 --> 00:08:42.140 but that's definitely valid code there anyway. 161 00:08:42.140 --> 00:08:45.350 So, you can see that it's working, and it's only returning the lines 162 00:08:45.350 --> 00:08:49.850 that have got the word Jabberwalk in, and printing them to the screen. 163 00:08:49.850 --> 00:08:53.370 Remember that string's just a sequence of characters, so as a result, 164 00:08:53.370 --> 00:08:55.720 we can test for the sequence general being in the lines. 165 00:08:55.720 --> 00:08:57.510 You can see that I've done a line four. 166 00:08:57.510 --> 00:09:00.660 Now, we haven't specifically covered a string's lower method to ensure that we 167 00:09:00.660 --> 00:09:04.010 find Jabberwock whether it's in upper or lowercase or a mix of both. 168 00:09:04.010 --> 00:09:08.590 Now, I mentioned earlier that we're using .lower, so that way, 169 00:09:08.590 --> 00:09:13.290 we're testing whether the string Jabberwock Text is upper or lower case. 170 00:09:13.290 --> 00:09:16.000 We're still gonna find a match because we've typed what we're searching for 171 00:09:16.000 --> 00:09:16.990 in lowercase. 172 00:09:16.990 --> 00:09:20.160 And we're using the .ToLower method, which is from the string class. 173 00:09:20.160 --> 00:09:23.900 and that's enabling us to do a comparison in respective of the case that's being 174 00:09:23.900 --> 00:09:25.730 typed in for the. 175 00:09:25.730 --> 00:09:28.990 And what the n did, was it specified that it should not automatic line 176 00:09:28.990 --> 00:09:31.060 after each line was printed, and you saw that when I ran it. 177 00:09:32.140 --> 00:09:34.180 You didn't space it out like the other examples we're showing. 178 00:09:34.180 --> 00:09:36.540 And the reason we're doing that is because the value of line, 179 00:09:36.540 --> 00:09:39.480 as it's read from the file, contains the new line character 180 00:09:39.480 --> 00:09:42.130 that is in the file itself to move on to the new line. 181 00:09:42.130 --> 00:09:46.115 Which is why earlier on when I was running this display, In the entire file, 182 00:09:46.115 --> 00:09:47.755 we've got double spacing. 183 00:09:47.755 --> 00:09:52.665 That's specifying n equals and basically two single quotes keeps print from 184 00:09:52.665 --> 00:09:56.635 printing another two-line character, which in this example is perfect. 185 00:09:56.635 --> 00:09:57.865 And finally of course on line seven, 186 00:09:57.865 --> 00:10:00.920 jabberwock close closes the file To tidy everything up. 187 00:10:00.920 --> 00:10:01.750 Very important to do that. 188 00:10:01.750 --> 00:10:05.340 In fact, failing to close a file after reading from it can result in subsequent 189 00:10:05.340 --> 00:10:07.620 attempts to read or write to that file to fail. 190 00:10:07.620 --> 00:10:10.870 And if you fail to close a file after writing to it, 191 00:10:10.870 --> 00:10:14.990 then the file itself can become corrupted and not all data can be written to it. 192 00:10:14.990 --> 00:10:18.930 Now, with this example, there's an even more Pythonic way to open files that, 193 00:10:18.930 --> 00:10:21.740 as well as being more concise, also removes the risk of 194 00:10:21.740 --> 00:10:25.110 failing to close the file afterwards, which is always a good thing. 195 00:10:25.110 --> 00:10:30.100 So, Python has a width statement that was introduced initially in Python 2.5, 196 00:10:30.100 --> 00:10:34.380 and if an object supports a use of width, and a file does, then width 197 00:10:34.380 --> 00:10:37.700 will tidy everything up automatically once the object's no longer needed. 198 00:10:37.700 --> 00:10:41.880 So looking at the previous example, let's just comment that out and 199 00:10:41.880 --> 00:10:43.340 let's top it using width. 200 00:10:43.340 --> 00:10:48.835 So with the top open, sample dot text, 201 00:10:48.835 --> 00:10:54.554 [INAUDIBLE]. 202 00:10:55.570 --> 00:10:56.090 Test java. 203 00:10:57.970 --> 00:10:58.872 Full line in java. 204 00:10:58.872 --> 00:11:04.077 If in line 205 00:11:07.092 --> 00:11:13.720 print line end equals Single quotes again. 206 00:11:13.720 --> 00:11:16.270 And again, we're ignoring that there because it's not really an error. 207 00:11:16.270 --> 00:11:18.580 It's just showing red for some reason. 208 00:11:18.580 --> 00:11:21.370 Okay, so run it, and you can see we got the same result. 209 00:11:21.370 --> 00:11:25.070 But there's no need to specifically close the folder this time because the width 210 00:11:25.070 --> 00:11:26.850 takes care of that for us. 211 00:11:26.850 --> 00:11:30.410 You'll notice also the way that these open files are associated with a variable 212 00:11:30.410 --> 00:11:31.890 by using S Jabber. 213 00:11:31.890 --> 00:11:33.940 So that we can refer to jabber in the loop, so 214 00:11:33.940 --> 00:11:37.470 you have to use the earlier myth that if you're writing parse and to code and 215 00:11:37.470 --> 00:11:40.360 also in that case to remember to close the file when you are done with it. 216 00:11:40.360 --> 00:11:42.140 This has got an additional advantage. 217 00:11:42.140 --> 00:11:45.092 Without using with, if there was an error in reading the file, 218 00:11:45.092 --> 00:11:48.400 The with stopping the file may not be closed, which, 219 00:11:48.400 --> 00:11:51.850 in a especially could mean that it can't be deleted or moved. 220 00:11:51.850 --> 00:11:53.530 So programs often have to areas and 221 00:11:53.530 --> 00:11:56.890 ensure that things like files are closed if an error in fact does occur. 222 00:11:56.890 --> 00:11:59.760 Now we're going to be looking at error soon. 223 00:11:59.760 --> 00:12:03.090 But using width here means that it's not something we need to worry about. 224 00:12:03.090 --> 00:12:05.840 If there's an error reading from the file, then the width statement will 225 00:12:05.840 --> 00:12:09.650 the exception, and close the file before the error terminates the program, and 226 00:12:09.650 --> 00:12:12.220 that's pretty cool that's handling all that for us. 227 00:12:12.220 --> 00:12:13.300 So, from now on in the course, 228 00:12:13.300 --> 00:12:15.810 we're going to be using width whenever an object supports it. 229 00:12:15.810 --> 00:12:18.020 So there are plenty of examples to get used to using that, and 230 00:12:18.020 --> 00:12:20.460 we do suggest that you do use it as much as possible. 231 00:12:20.460 --> 00:12:22.380 Now, before we move on to look at different modes for 232 00:12:22.380 --> 00:12:25.040 opening a file, we'll have a look at another couple of ways we 233 00:12:25.040 --> 00:12:29.580 could've written the first program to show the use of read, read line and read lines. 234 00:12:29.580 --> 00:12:33.632 SO, i'm gonna close this down and we can also change this. 235 00:12:33.632 --> 00:12:37.930 We got could've put line equals. 236 00:12:37.930 --> 00:12:41.400 And so what i'll do is what I done before 237 00:12:41.400 --> 00:12:44.490 commenting code you got the various code you use in each lecture. 238 00:12:44.490 --> 00:12:47.455 So, we'll start again with open. 239 00:12:47.455 --> 00:12:49.640 Sample.text again. 240 00:12:51.370 --> 00:12:56.934 Read only, S jabber, and this one we'll talk line equals jabber 241 00:12:56.934 --> 00:13:02.657 .readline while 242 00:13:02.657 --> 00:13:07.622 line, print line. 243 00:13:07.622 --> 00:13:12.024 And we talk line equals jabber.readline. 244 00:13:13.320 --> 00:13:15.770 Like so. And if you run that 245 00:13:15.770 --> 00:13:17.680 you can see we've got the whole poem this time. 246 00:13:17.680 --> 00:13:21.320 And notice that it has no double spacing anymore because 247 00:13:21.320 --> 00:13:23.720 the end=' ' has removed that extra line. 248 00:13:23.720 --> 00:13:28.300 Because, of course, there was an end line in the actual text file itself. 249 00:13:28.300 --> 00:13:29.568 And so, what we're saying is. 250 00:13:29.568 --> 00:13:35.880 By putting n="" or an empty string, we're saying don't print that line as well, 251 00:13:35.880 --> 00:13:37.930 and that's why we're not getting that double up anymore. 252 00:13:37.930 --> 00:13:41.270 And in terms of using red line, you can see that we read the first line of 253 00:13:41.270 --> 00:13:45.980 the file on line 14, Into the variable line, and if some data be read, 254 00:13:45.980 --> 00:13:48.720 in other words, the file wasn't empty, then we enter the wallet. 255 00:13:48.720 --> 00:13:49.670 Now, if there was a problem, 256 00:13:49.670 --> 00:13:54.430 and the file was empty, it wouldn't process from line 15 onwards. 257 00:13:54.430 --> 00:13:55.875 It wouldn't get into the wall, in other words. 258 00:13:55.875 --> 00:14:00.005 At that point the while loop is gonna continue until there's no lines, 259 00:14:00.005 --> 00:14:01.895 there's no additional lines to read. 260 00:14:01.895 --> 00:14:05.375 And this code is probably closer to the way that a file is normally processed in 261 00:14:05.375 --> 00:14:08.405 other programming languages, sort of line by line as you can see there. 262 00:14:09.547 --> 00:14:14.197 And as another example I'll just take a copy of that and then comment that out. 263 00:14:17.877 --> 00:14:20.077 And then down here posted it again. 264 00:14:20.077 --> 00:14:23.038 There's also another example, we could use lines. 265 00:14:23.038 --> 00:14:27.418 this case, lines equals dot read lines. 266 00:14:27.418 --> 00:14:30.678 And we can literally then just get rid of the y altogether. 267 00:14:31.728 --> 00:14:33.898 Change the print just to print lines. 268 00:14:33.898 --> 00:14:34.748 We don't need the end any more. 269 00:14:34.748 --> 00:14:38.068 And we don't need the line either. 270 00:14:38.068 --> 00:14:41.808 So that's going to read the entire file and return the list, 271 00:14:41.808 --> 00:14:45.478 which can then be processed automatically as ever we would like. 272 00:14:45.478 --> 00:14:47.260 As any other list for that matter. 273 00:14:47.260 --> 00:14:49.270 So I could do that first, run it. 274 00:14:49.270 --> 00:14:51.540 You can see that's been converted to a list. 275 00:14:51.540 --> 00:14:56.060 And then what we could also do then, is we can put four lines using a standard list, 276 00:14:57.090 --> 00:14:59.131 loop four line in lines. 277 00:14:59.131 --> 00:15:05.135 Print line and equals single quote, single quote. 278 00:15:05.135 --> 00:15:08.395 And if we run that, we get a similar thing there. 279 00:15:08.395 --> 00:15:12.020 We've got going through the list element by element effectively. 280 00:15:12.020 --> 00:15:13.140 And pretty up your typo. 281 00:15:13.140 --> 00:15:15.160 And the other thing I wanted to point out, just run that again, 282 00:15:15.160 --> 00:15:17.190 can you see the slash end there? 283 00:15:17.190 --> 00:15:18.670 This second entry there. 284 00:15:18.670 --> 00:15:21.620 The slash end there is the sort of end of line character. 285 00:15:21.620 --> 00:15:23.340 And that's on each element so 286 00:15:23.340 --> 00:15:25.950 that's why we need to use the end equals single quote, single quote. 287 00:15:25.950 --> 00:15:30.850 Or entry string, in other words, to avoid that being processed and doubling us up. 288 00:15:30.850 --> 00:15:34.640 So that's In the file itself, which you would expect when you're creating a file. 289 00:15:34.640 --> 00:15:35.900 When you press Enter, that's what happens, 290 00:15:35.900 --> 00:15:38.590 the file normally includes that character in there. 291 00:15:38.590 --> 00:15:39.810 That does bring up an interesting point. 292 00:15:39.810 --> 00:15:42.520 It is very useful, when you're de-bugging, to print things like this, 293 00:15:42.520 --> 00:15:45.480 because you may not have been aware of that, for argument's sake. 294 00:15:45.480 --> 00:15:49.580 And just seeing that that's in the data itself that's returned 295 00:15:49.580 --> 00:15:53.690 from a read lines method can be really useful for de-bugging purposes. 296 00:15:53.690 --> 00:15:59.080 So printing variables as a rule like this is very useful, and you really see what 297 00:15:59.080 --> 00:16:02.370 the data is, because it may or may not be what you had expected, and obviously, 298 00:16:02.370 --> 00:16:05.730 you would never leave that type of output in a finished program, but for 299 00:16:05.730 --> 00:16:08.670 debugging purposes, while you're developing it, testing it, 300 00:16:08.670 --> 00:16:11.400 debugging it, it's really useful to see what's going on. 301 00:16:11.400 --> 00:16:14.070 So, read lines, let's close that down, 302 00:16:14.070 --> 00:16:16.730 read lines reads the entire file in one go. 303 00:16:16.730 --> 00:16:20.720 Now this may not be a good idea for a very large file, but can have interesting uses, 304 00:16:20.720 --> 00:16:21.910 depending on the file itself. 305 00:16:21.910 --> 00:16:24.170 So what we could have done, and I'll just copy this again, 306 00:16:29.760 --> 00:16:34.290 down here we can put four line in lines, and then we'll change that to a range, and 307 00:16:34.290 --> 00:16:40.710 we'll put minus one, so, and now if we run that That might appear 308 00:16:40.710 --> 00:16:45.290 to be a bit frivolous, but it really demonstrates what readLines is doing. 309 00:16:45.290 --> 00:16:48.190 By iterating through a reverse slice you can see that 310 00:16:48.190 --> 00:16:49.750 we do indeed have a list of lines. 311 00:16:49.750 --> 00:16:53.520 So you contrast that with using read in this next example and 312 00:16:53.520 --> 00:16:56.860 the instructions to print the value of the lines variable is being removed. 313 00:16:56.860 --> 00:16:59.975 So contrast that with this code I'm about to write here now. 314 00:16:59.975 --> 00:17:03.840 [SOUND] So we'll do the same thing again. 315 00:17:03.840 --> 00:17:07.280 This time I'll leave that line as well. 316 00:17:07.280 --> 00:17:07.810 We'll get rid of that. 317 00:17:07.810 --> 00:17:08.940 Leave the print lines out there. 318 00:17:08.940 --> 00:17:12.310 And we'll change this to read instead of read lines, just read. 319 00:17:14.050 --> 00:17:17.970 And run that. And you can see now the difference. 320 00:17:17.970 --> 00:17:20.650 And the instructions will print the value of the lines variables being removed to 321 00:17:20.650 --> 00:17:22.920 make it clear what's being reversed, as you can see there. 322 00:17:22.920 --> 00:17:25.472 So that's been completely reversed using .read. 323 00:17:25.472 --> 00:17:26.225 So read. 324 00:17:26.225 --> 00:17:28.755 Are really more useful dealing with binary files. 325 00:17:28.755 --> 00:17:32.085 But this does demonstrate that the way we've used it here to read the entire file 326 00:17:32.085 --> 00:17:33.645 into a string in one go, which is, 327 00:17:33.645 --> 00:17:37.235 of course, another way of, of manipulating files. 328 00:17:37.235 --> 00:17:39.810 Readlines also reads the entire file. 329 00:17:39.810 --> 00:17:42.870 But gives a list of strings rather than just a single string. 330 00:17:42.870 --> 00:17:45.970 So just to summarize before we finish this video, 331 00:17:45.970 --> 00:17:48.860 readline reads a single line from a file and returns a string. 332 00:17:48.860 --> 00:17:52.880 Now this is the same behavior as we did when forline in Java, 333 00:17:52.880 --> 00:17:56.130 which is at the start, one of the first things we did in this lecture. 334 00:17:56.130 --> 00:17:58.470 The files read one line at a time in other words. 335 00:17:58.470 --> 00:18:01.240 The latch file this is the recommended approach and for 336 00:18:01.240 --> 00:18:04.290 obvious reasons because it doesn't read the entire file into memory and depending 337 00:18:04.290 --> 00:18:07.770 on your devices and the computer you may not have a lot of memory to work with. 338 00:18:07.770 --> 00:18:08.820 Now read lines. 339 00:18:08.820 --> 00:18:11.640 Reads the entire file and returns a list of strings. 340 00:18:11.640 --> 00:18:15.600 So, the entire file is readed into memory which can again cause problems if you 341 00:18:15.600 --> 00:18:17.820 got very last files occupying a lot of memory. 342 00:18:17.820 --> 00:18:20.580 And read, as we last used it on the last 343 00:18:20.580 --> 00:18:24.150 method there on the last example Raising tar file and 344 00:18:24.150 --> 00:18:27.330 if it's a text file returns a string containing the contents of the file. 345 00:18:27.330 --> 00:18:30.170 So read can also take an optional parameter 346 00:18:30.170 --> 00:18:33.110 specifying how much data to read, and we'll see that in use later. 347 00:18:33.110 --> 00:18:35.850 So that's it, I'm going to finish the video here now. 348 00:18:35.850 --> 00:18:39.190 In the next video we're going to start talking about writing to a text file. 349 00:18:39.190 --> 00:18:40.380 See you in that next video.