1
00:00:05,200 --> 00:00:10,000
We've seen how to read lines from a text 
file, by iterating over the file object. 

2
00:00:10,000 --> 00:00:14,320
The open function returns a file object, and 
iterating over it is probably the most common 

3
00:00:14,320 --> 00:00:18,720
way to process the lines in a text file.
But Python provides 3 other methods 

4
00:00:18,720 --> 00:00:22,347
for reading from a file, and we'll 
have a quick look at each of them. 

5
00:00:22,347 --> 00:00:27,486
I'll continue with our read_poem program, 
and start with the readlines method. 

6
00:00:28,560 --> 00:00:31,840
Back in IntelliJ:
Comment out the for loop, 

7
00:00:31,840 --> 00:00:34,597
and we'll see how readlines can be useful. 

8
00:00:43,120 --> 00:00:48,080
The readlines method returns a list, 
containing strings for each line from the file. 

9
00:00:48,080 --> 00:00:52,560
So we now have the entire file stored in a list.
We've read the entire file, 

10
00:00:52,560 --> 00:00:56,329
and can come out of the with block – 
allowing Python to close the file: 

11
00:01:02,400 --> 00:01:08,025
Before we discuss why we might want to do this, 
let's run the program, to make sure it works. 

12
00:01:09,600 --> 00:01:15,200
As you can see, the output is a list of strings.
If you were unsure about the newline characters, 

13
00:01:15,200 --> 00:01:19,680
in the last video, notice that Python 
displays them as \n, when it prints a 

14
00:01:19,680 --> 00:01:25,600
structure such as a list. We can see the 
\n characters after "toves"and "wabe". 

15
00:01:25,600 --> 00:01:30,320
We can also see the string that contains just a 
newline, between "And the mome wraths outgrabe." 

16
00:01:30,320 --> 00:01:35,425
and "Beware the Jabberwock". That's the blank 
line between those two blocks of the poem. 

17
00:01:35,680 --> 00:01:39,280
So why might we want to do this?
Having the entire file in memory 

18
00:01:39,280 --> 00:01:43,120
lets us do some things that would be difficult 
when processing the file line-by-line. 

19
00:01:43,120 --> 00:01:49,585
We can index a list, for example. So we could 
get the last line of the file, using a slice: 

20
00:01:59,280 --> 00:02:00,862
Run the program 

21
00:02:02,400 --> 00:02:06,240
and we get the credit for the poem 
– it was written by Lewis Carroll. 

22
00:02:06,240 --> 00:02:09,840
If you wanted to process the files 
in reverse order, for some reason, 

23
00:02:09,840 --> 00:02:12,582
you can use a reverse iterator:

24
00:02:29,659 --> 00:02:32,221
Run the program again

25
00:02:33,200 --> 00:02:40,000
and the poem's printed out backwards. Of course, 
you can do any processing that's appropriate for your task. 

26
00:02:40,000 --> 00:02:44,243
I've just printed the lines, but you 
may want to manipulate each line in some way. 

27
00:02:44,320 --> 00:02:47,840
At the moment, we're looking at how to 
read from a text file – we're not yet 

28
00:02:47,840 --> 00:02:53,120
doing anything useful with the data that we read.
There is a serious disadvantage to processing a 

29
00:02:53,120 --> 00:02:58,560
file like this, that you need to consider.
We've read every single line into a list. 

30
00:02:58,560 --> 00:03:02,800
That works fine for this short "Jabberwocky" 
file, but what if you're trying to process 

31
00:03:02,800 --> 00:03:08,113
a file that contains millions of lines?
The file might be too big to fit into memory. 

32
00:03:08,113 --> 00:03:13,840
As the documentation says, "it's your problem if the 
file is twice as large as your machine's memory". 

33
00:03:13,840 --> 00:03:18,160
readlines can be useful, when dealing 
with files that will fit into memory. 

34
00:03:18,160 --> 00:03:22,311
But make sure you understand the implications 
of trying to read an entire file. 

35
00:03:22,311 --> 00:03:26,800
That also applies to the read function. 
Unlike readlines, read doesn't split the 

36
00:03:26,800 --> 00:03:32,327
lines up. Instead, it returns a single string 
containing all the characters in the file. 

37
00:03:32,480 --> 00:03:36,058
I'll comment out all that 
code, and use read instead: 

38
00:04:01,360 --> 00:04:03,503
Run this code: 

39
00:04:05,280 --> 00:04:09,840
The output looks the same, but we've 
now got a string, rather than a list. 

40
00:04:09,840 --> 00:04:15,437
That becomes obvious when we come to process 
text. I'll perform a reverse iteration again: 

41
00:04:33,440 --> 00:04:39,840
Run the program again, and check the output.
Because we've reversed a string this time, 

42
00:04:39,840 --> 00:04:43,840
rather than a list, the entire 
contents of the file are reversed. 

43
00:04:43,840 --> 00:04:47,760
You'll find read useful if you wanted to 
perform a search and replace on the entire file 

44
00:04:47,760 --> 00:04:52,000
– to give one example. Because the 
entire file is in a single string, 

45
00:04:52,000 --> 00:04:58,320
it's easy to process all the text in its entirety.
I'll finish this video with the readline method. 

46
00:04:58,320 --> 00:05:03,520
I've left it till last, because you probably 
won't use it very often. We'll see what it does, 

47
00:05:03,520 --> 00:05:07,040
then I'll explain why it's not 
as useful as read and readlines. 

48
00:05:07,040 --> 00:05:11,956
Comment out our existing code, and we'll 
loop through the file using readlines: 

49
00:05:52,560 --> 00:05:56,480
Here, we read the file line-by-line, and 
break out of the loop when we find something 

50
00:05:56,480 --> 00:05:59,848
we're looking for.
Run the program: 

51
00:06:01,200 --> 00:06:04,752
and the program terminates when it 
finds the line containing "Jubjub". 

52
00:06:04,960 --> 00:06:09,384
That's one use for readline. It 
reads one line of the file at a time. 

53
00:06:09,600 --> 00:06:13,760
The reason you might not use it very often, is 
that we can get the same result by just iterating 

54
00:06:13,760 --> 00:06:18,238
over the file object – which is what we did when 
we first looked at reading from a text file. 

55
00:06:18,238 --> 00:06:20,519
Here's the equivalent code: 

56
00:06:59,200 --> 00:07:05,191
There's not a lot to choose from, between these 
two loops. They both behave very similarly. 

57
00:07:05,191 --> 00:07:09,840
You may want to use the first loop – with readline 
– if you want to process the string in some way, 

58
00:07:09,840 --> 00:07:13,200
and work with the processed string 
throughout the remainder of the code. 

59
00:07:13,200 --> 00:07:17,457
For example, on line 28, we strip the 
newline from the end of the string, 

60
00:07:17,457 --> 00:07:22,160
before binding the variable line to it.
Compare that to line 37, where we also 

61
00:07:22,160 --> 00:07:27,760
strip the newline, but only for printing.
The test, on line 38, is performed with 

62
00:07:27,760 --> 00:07:33,760
a string that still contains the newline.
Strings are immutable, remember. So line 37 

63
00:07:33,760 --> 00:07:38,258
doesn't mutate the string – it just prints 
a copy that doesn't end with a newline. 

64
00:07:38,720 --> 00:07:42,205
Ok, I'll finish this video with 
something that you may have noticed. 

65
00:07:42,400 --> 00:07:46,569
I'll scroll up to the top of the document, 
and have a look at lines 1 and 9. 

66
00:07:46,640 --> 00:07:51,440
When I called the open function, I passed 
the name of the file – "Jabberwocky.txt" 

67
00:07:51,440 --> 00:07:56,400
– and the mode that I wanted to open 
the file. I used "r", for "read". 

68
00:07:56,400 --> 00:08:02,489
Scrolling down, I didn't specify 
the mode, on lines 19, 26 and 35. 

69
00:08:02,560 --> 00:08:07,369
The default mode, if you don't specify 
one, is to open the file for reading. 

70
00:08:07,440 --> 00:08:10,673
So that's why I left "r" out 
of those three function calls. 

71
00:08:10,800 --> 00:08:14,720
We'll see the different modes you 
can use, a bit later in this section. 

72
00:08:14,720 --> 00:08:19,200
In the next video, I'm going to digress, 
and talk about the strip string methods: 

73
00:08:19,200 --> 00:08:23,760
strip, lstrip, and rstrip.
See you in the next video.

