1
00:00:05,200 --> 00:00:10,960
Use your IDE to create a new project for this 
section. I've called my project TextFiles. 

2
00:00:10,960 --> 00:00:15,440
Before we can read from a text file, 
we're going to need a file to read from. 

3
00:00:15,440 --> 00:00:18,400
Download Jabberwocky.txt from 
the resources for this video, 

4
00:00:18,400 --> 00:00:23,420
and save it into your project directory.
You can save it anywhere you want in fact. 

5
00:00:23,420 --> 00:00:28,768
But if you put the file somewhere else, you'll need 
to provide the full path to it, when we open it. 

6
00:00:28,768 --> 00:00:32,880
I'll be discussing paths in Python, but if 
you want to use a full path before then, 

7
00:00:32,880 --> 00:00:37,600
remember that the backslash character is 
the escape character, in Python strings. 

8
00:00:37,600 --> 00:00:42,932
Use the slash instead. That will 
work in Python code, even on Windows. 

9
00:00:43,360 --> 00:00:47,440
Ok, let's see how to read in 
the contents of Jabberwocky.txt. 

10
00:00:47,440 --> 00:00:51,040
When you want to work with a 
file, you have to open it first. 

11
00:00:51,040 --> 00:00:55,360
We want to read from the file, so we open 
it for reading. It's also possible to 

12
00:00:55,360 --> 00:01:02,649
write to a file, and we'll look at that next.
Create a new Python file called read_poem.py. 

13
00:01:08,240 --> 00:01:12,361
To open a file, we use
the open function:

14
00:01:24,355 --> 00:01:28,089
This isn't the cleanest way to
open a file, in Python.

15
00:01:28,089 --> 00:01:32,552
PEP 343 introduced the with statement,
for working with things like files.

16
00:01:32,552 --> 00:01:38,040
The with statement was added to the language in Python 2.5, 
and we'll be opening all our files using with, 

17
00:01:38,040 --> 00:01:44,160
once we've seen what it does. At the moment, we need
to understand some of the basics of working with files. 

18
00:01:44,160 --> 00:01:46,960
If we start with with, you'll miss 
out on the concept of closing a 

19
00:01:46,960 --> 00:01:50,480
file when you've finished with it.
So for now, don't take this as the 

20
00:01:50,480 --> 00:01:55,440
best way to open a file – think of it 
as a way to understand how files work. 

21
00:01:55,440 --> 00:01:58,880
Back in IntelliJ, have a 
look at our code on line 1. 

22
00:01:58,880 --> 00:02:03,040
The open function returns an object that 
we can use to get data from the file. 

23
00:02:03,040 --> 00:02:07,520
In this example, we bind the object to a 
variable called jabber. We could have used 

24
00:02:07,520 --> 00:02:12,640
any name for that, but as the poem's called 
Jabberwocky, that seemed like a good name. 

25
00:02:12,640 --> 00:02:15,760
The first argument we pass to open, 
is the name of the file that we 

26
00:02:15,760 --> 00:02:21,920
want to open. That's Jabberwocky.txt, here.
Because I put the text file into my project 

27
00:02:21,920 --> 00:02:27,520
directory, I haven't had to specify the full 
path. If you're still unsure about file paths, 

28
00:02:27,520 --> 00:02:32,320
keep your text files in the project directory, 
until you've learnt more about paths. 

29
00:02:32,320 --> 00:02:37,920
The second argument is 'r', which stands for read.
We can read from this file, but we can't write 

30
00:02:37,920 --> 00:02:42,960
to it. We'll open a file for writing, 
once we've seen how to read from a file. 

31
00:02:42,960 --> 00:02:46,560
For now, let's see what we can 
do with the object it returns. 

32
00:02:46,560 --> 00:02:51,675
Text files contain lines of text, and we 
can iterate over jabber, to get each line: 

33
00:03:06,320 --> 00:03:11,440
The for loop binds each line from the file, 
to the variable line, then prints it out. 

34
00:03:11,440 --> 00:03:16,158
Importantly, we have to close the file 
when we're finished – that's on line 6. 

35
00:03:16,240 --> 00:03:20,080
If you don't close a file when you're 
finished with it, you can get problems. 

36
00:03:20,080 --> 00:03:25,280
That's especially true on Windows, but is also 
very important on other operating systems. 

37
00:03:25,280 --> 00:03:30,960
It's so important, and a common cause of problems, 
that Python introduced another way to open files 

38
00:03:30,960 --> 00:03:34,960
– the with statement that I mentioned earlier.
We'll change this code to use with, 

39
00:03:34,960 --> 00:03:41,543
in a moment. First, let's see 
if it works. Run the program: 

40
00:03:42,560 --> 00:03:46,400
and we get each line of the file printed out.
Scroll up through the output, 

41
00:03:46,400 --> 00:03:51,600
to check that the whole file has been printed.
Because this is a text file, we can open it in our 

42
00:03:51,600 --> 00:03:59,840
editor, to check. Double click Jabberwocky.txt 
in the Project pane, to open it in IntelliJ. 

43
00:04:01,200 --> 00:04:05,520
That looks very similar to our output, 
but why is our output double spaced? 

44
00:04:05,520 --> 00:04:10,480
The reason for that is, there's a newline 
character at the end of each line, in the file. 

45
00:04:10,480 --> 00:04:16,892
We've seen the newline before, a few times. 
We represent it in our Python strings with \n. 

46
00:04:16,892 --> 00:04:20,499
When text editors see a newline 
in the file, they don't print it – 

47
00:04:20,499 --> 00:04:25,040
they start a new line instead.
With Jabberwocky.txt open in IntelliJ, 

48
00:04:25,040 --> 00:04:29,865
position your cursor just before the s 
of toves, at the end of the first line. 

49
00:04:30,720 --> 00:04:35,120
When you press the right cursor key, 
the cursor moves right one position. 

50
00:04:35,120 --> 00:04:40,284
That puts it after the letter "s".
Now press the right cursor key again. 

51
00:04:40,720 --> 00:04:45,328
That causes the cursor to move over the next 
character – which is the newline character. 

52
00:04:45,328 --> 00:04:48,398
The cursor moves to the start of the next line. 

53
00:04:48,480 --> 00:04:52,480
So although you can't see it, there's an 
extra character at the end of each line. 

54
00:04:52,480 --> 00:04:57,840
You can't see the character, you see its behaviour 
instead – it causes a new line to be started. 

55
00:04:57,840 --> 00:05:01,520
The print function defaults 
to printing \n when it prints. 

56
00:05:01,520 --> 00:05:04,880
So we have a newline printed because 
each line of text ends with a newline, 

57
00:05:04,880 --> 00:05:09,040
and we get another newline printed 
automatically, by the print function. 

58
00:05:09,040 --> 00:05:10,720
There are two ways you can deal with that, 

59
00:05:10,720 --> 00:05:13,760
depending on what you want to 
do with the lines from the file. 

60
00:05:13,760 --> 00:05:17,440
If you just want to print each line, as 
we're doing here, you can tell print 

61
00:05:17,440 --> 00:05:23,840
not to include a newline after printing. Switch 
back to read_poem.py to make the change: 

62
00:05:30,400 --> 00:05:35,200
We've used the end keyword argument before. You 
can include extra text to be printed at the end 

63
00:05:35,200 --> 00:05:40,240
of the output; or you can use it to suppress 
the automatic newline – as we're doing here. 

64
00:05:40,240 --> 00:05:42,474
Run the program: 

65
00:05:45,200 --> 00:05:48,800
and we no longer get the extra 
newline between each line of text. 

66
00:05:48,800 --> 00:05:54,240
Each line ends with a newline, as we've 
just seen, in Jabberwocky.txt. That gets 

67
00:05:54,240 --> 00:05:59,440
printed as part of each line, so we suppress the 
automatic newline that print adds by default. 

68
00:05:59,440 --> 00:06:04,640
That's one way. The other way is to strip off 
the newline from each line of text that we read. 

69
00:06:04,640 --> 00:06:08,560
We're reading strings, so the 
variable line is bound to a string. 

70
00:06:08,560 --> 00:06:10,320
Python strings have a strip method, 

71
00:06:10,320 --> 00:06:14,510
that strips all whitespace from the 
beginning and end of the string. 

72
00:06:15,200 --> 00:06:19,389
Whitespace is any character that 
represents horizontal or vertical spacing. 

73
00:06:19,389 --> 00:06:24,080
Whitespace characters aren't visible as 
characters, but they do space things out. 

74
00:06:24,080 --> 00:06:27,120
Examples of whitespace characters,
in programming, 

75
00:06:27,120 --> 00:06:28,960
are:
space 

76
00:06:28,960 --> 00:06:31,604
tab
newline 

77
00:06:33,440 --> 00:06:37,440
Note: These are characters, just 
like any other. The first line 

78
00:06:37,440 --> 00:06:42,880
of the file contains the text below:
but the string contains 36 characters. 

79
00:06:42,880 --> 00:06:47,520
There are 5 spaces and a newline, in 
addition to the characters we can see. 

80
00:06:47,520 --> 00:06:52,507
Let's check that, in IntelliJ, by 
printing the length of each line: 

81
00:07:04,000 --> 00:07:07,680
Run the program, and scroll back 
up to the start of the output. 

82
00:07:07,680 --> 00:07:12,880
The length of the first line is 36.
There were 36 characters read from the file, 

83
00:07:12,880 --> 00:07:17,840
when we read the first line.
Ok, back to the strip method. 

84
00:07:17,840 --> 00:07:22,160
If you haven't already, check out the 
documentation on the various string methods. 

85
00:07:22,160 --> 00:07:26,960
I'll digress slightly, in an upcoming 
video, and talk more about strip. 

86
00:07:26,960 --> 00:07:31,600
It's not immediately obvious exactly how 
it behaves, when you pass a string to it. 

87
00:07:31,600 --> 00:07:35,840
We're going to use it without arguments, 
so I'll leave that discussion until later. 

88
00:07:35,840 --> 00:07:40,488
For now, we'll strip off all whitespace from the 
beginning and end of the strings that we read: 

89
00:07:50,560 --> 00:07:55,115
I'll also comment out line 5, because 
we don't need those counts any more. 

90
00:07:55,115 --> 00:07:59,120
You've already counted the characters in a 
few lines, to prove that they end in newline, 

91
00:07:59,120 --> 00:08:02,451
haven't you?
Run the program: 

92
00:08:03,760 --> 00:08:08,400
and you may not have expected the result.
We get one long line of text. 

93
00:08:08,400 --> 00:08:10,720
We stripped the newline 
character from each string, 

94
00:08:10,720 --> 00:08:15,840
and also told print not to include a newline.
For our purposes, we want to use one or the 

95
00:08:15,840 --> 00:08:21,757
other of these techniques – not both!
I'll remove the end argument from print: 

96
00:08:24,320 --> 00:08:28,320
Run the program again:
and we get each line printed, 

97
00:08:28,320 --> 00:08:33,919
with a single newline separating each one.
So that's two ways to deal with our strings. 

98
00:08:33,919 --> 00:08:37,736
We can suppress the newline that print 
automatically adds for us, or we can 

99
00:08:37,736 --> 00:08:42,505
remove the newline character from the ends of 
the strings, after reading them from the file. 

100
00:08:42,799 --> 00:08:46,240
I'll finish this video with a comment 
about the last line of the output. 

101
00:08:46,240 --> 00:08:50,240
If you've scrolled the output, scroll 
back so that the last line is visible. 

102
00:08:50,240 --> 00:08:55,404
Notice that the first character is supposed to 
be an m-dash, but it's displaying strangely. 

103
00:08:55,404 --> 00:09:00,640
I'll explain about that, when we look at file encoding.
At the moment, notice that those strange 

104
00:09:00,640 --> 00:09:04,105
characters are against the left margin 
– just like all the other lines. 

105
00:09:04,240 --> 00:09:08,320
We've stripped the whitespace from the 
start of each line, as well as the end. 

106
00:09:08,320 --> 00:09:12,677
If that's not the behaviour you 
want, you can use rstrip instead: 

107
00:09:13,600 --> 00:09:17,360
rstrip strips from the right – the 
end of the string in other words. 

108
00:09:17,360 --> 00:09:22,505
There's also an lstrip method, that strips 
from the start of the string – the left. 

109
00:09:23,760 --> 00:09:26,240
If you're using a language that's 
written from right to left, 

110
00:09:26,240 --> 00:09:31,680
these methods may not work as their names suggest.
The documentation does refer to "leading" 

111
00:09:31,680 --> 00:09:35,840
and "trailing" characters, for lstrip 
and rstrip respectively. 

112
00:09:37,840 --> 00:09:41,920
As far as we can tell from experimenting – and 
bearing in mind that we don't understand any 

113
00:09:41,920 --> 00:09:46,240
right-to-left language – lstrip will strip from 
the beginning of the string (the characters that 

114
00:09:46,240 --> 00:09:51,387
are written first), which is the right hand side.
rstrip strips from the end of the string, 

115
00:09:51,387 --> 00:09:56,638
which is the left hand side of a right-to-left language.
The important thing is that you know they're there. 

116
00:09:56,638 --> 00:10:01,356
Experiment, to work out the 
exact behaviour for your language. 

117
00:10:03,040 --> 00:10:07,600
When you're experimenting with the string 
methods, remember that strings are immutable. 

118
00:10:07,600 --> 00:10:12,400
str.strip() won't modify the string.
Remember to bind a variable to the result, 

119
00:10:12,400 --> 00:10:14,877
if you want to use the stripped string:

120
00:10:20,683 --> 00:10:24,336
Ok, run the program:

121
00:10:24,560 --> 00:10:29,360
and we get single line spacing, with the 
leading spaces on the last line preserved. 

122
00:10:29,360 --> 00:10:34,086
In the next video, we'll see a more
Pythonic way to open a file, using with.

123
00:10:34,086 --> 00:10:37,920
See you in the next video.

