1
00:00:05,300 --> 00:00:08,800
So this section, we're gonna look
at input and output in Python.

2
00:00:08,800 --> 00:00:12,230
So, I/O, input output just means
getting data into a program and

3
00:00:12,230 --> 00:00:13,490
getting it out again.

4
00:00:13,490 --> 00:00:17,110
Now, we've already seen one way to enter
data into our programs from the keyboard.

5
00:00:17,110 --> 00:00:20,450
But in this section we're gonna be looking
at another way that can also be useful.

6
00:00:20,450 --> 00:00:23,360
And we've also covered printing
results to the screen.

7
00:00:23,360 --> 00:00:27,350
But so far every time we've needed data
in our program, we've had to type it in.

8
00:00:27,350 --> 00:00:30,830
So wouldn't it be useful to be able
to save that data to a file so

9
00:00:30,830 --> 00:00:32,590
that we can use it over and over again?

10
00:00:32,590 --> 00:00:35,220
So in this section we're gonna be looking
at a number of ways to write data to

11
00:00:35,220 --> 00:00:37,840
a file using the computer's disk drive.

12
00:00:37,840 --> 00:00:41,360
And we'll also include writing
the data in a similar way,

13
00:00:41,360 --> 00:00:43,160
To how it's printed on the screen.

14
00:00:43,160 --> 00:00:47,830
This is a process you'll
understand a little bit later.

15
00:00:47,830 --> 00:00:51,360
Another process we're also going to look
at how python dictates object provide

16
00:00:51,360 --> 00:00:54,000
the basis for
a very useful way to store data.

17
00:00:54,000 --> 00:00:54,980
So, let's make a start.

18
00:00:54,980 --> 00:00:58,020
The first we're gonna talk
about reading text files.

19
00:00:58,020 --> 00:01:01,180
And the good thing about this is
that reading text files in Python

20
00:01:01,180 --> 00:01:05,209
is incredibly easy and just really
consists of three simple steps.

21
00:01:05,209 --> 00:01:06,590
So, first we have to open the file.

22
00:01:06,590 --> 00:01:10,600
And we And we use that using the Python
built in function called Open.

23
00:01:10,600 --> 00:01:13,360
Then the next step is to read in the file
which we can do either one line at

24
00:01:13,360 --> 00:01:17,500
a time or we can actually try and
read in the entire file in one go.

25
00:01:17,500 --> 00:01:20,740
Finally to close the file when we're
done with it is the final step.

26
00:01:20,740 --> 00:01:23,720
Now this is very important,
especially when writing to a file

27
00:01:23,720 --> 00:01:26,810
because if it's not done the file
can actually get corrupted.

28
00:01:26,810 --> 00:01:29,870
Now the resources section of this video
contains a text file that you can see on

29
00:01:29,870 --> 00:01:31,250
the screen there now.

30
00:01:31,250 --> 00:01:33,370
That we can use to
practice reading a file.

31
00:01:33,370 --> 00:01:36,700
So download that file, and place it in
a suitable directory in your local drive.

32
00:01:36,700 --> 00:01:39,460
It contains Lewis Carroll's
poem Jabberwocky,

33
00:01:39,460 --> 00:01:40,890
from through The Looking Glass.

34
00:01:40,890 --> 00:01:43,000
So we're going to use that,
and read that into Python.

35
00:01:43,000 --> 00:01:46,690
Now I've actually stored
this on my desktop.

36
00:01:46,690 --> 00:01:49,030
But you can literally store anywhere.

37
00:01:49,030 --> 00:01:53,050
So, let's go ahead now and write a program
that will read this file into Python.

38
00:01:53,050 --> 00:01:59,200
So, we're gonna start by typing Java = and

39
00:01:59,200 --> 00:02:04,140
We're gonna type open which is
the built in function to open a file.

40
00:02:04,140 --> 00:02:07,310
We're gonna put home/dev/Desktop.

41
00:02:07,310 --> 00:02:12,730
Now that's actually the path
that I've got on my computer

42
00:02:12,730 --> 00:02:17,220
to this file and it's called sample.txt.

43
00:02:17,220 --> 00:02:21,500
Like so, and then we can go ahead and
type for line in jober.

44
00:02:23,140 --> 00:02:30,180
Print line, and lastly, jabber.close.

45
00:02:30,180 --> 00:02:33,350
So again, the first line opens
the text file on line one.

46
00:02:33,350 --> 00:02:37,820
On line three and four, we're actually
cycling through each line in the document.

47
00:02:37,820 --> 00:02:39,610
Then we're printing it
out one line at a time.

48
00:02:39,610 --> 00:02:42,150
So we're cycling right through
to the end of the file.

49
00:02:42,150 --> 00:02:44,190
And line six closes at
the file when we're done.

50
00:02:44,190 --> 00:02:47,220
Now the other thing I didn't
put here which we actually need

51
00:02:47,220 --> 00:02:50,420
at the end of the open is we need
to provide the mode as well.

52
00:02:50,420 --> 00:02:53,340
So if you think about it when
you're opening a text file or

53
00:02:53,340 --> 00:02:57,760
a file on your computer you need to tell
it what type of input output you want.

54
00:02:57,760 --> 00:03:01,050
So in this case we don't want to write
to this file, we want to read to it.

55
00:03:01,050 --> 00:03:04,050
So to do that we just Put a comma and

56
00:03:04,050 --> 00:03:09,690
we put the letter R in single quotes,
which again stands for read-only or read.

57
00:03:09,690 --> 00:03:13,580
Now the thing here also is you can provide
a full path, as you can see on the screen

58
00:03:13,580 --> 00:03:16,920
here, or you can top a relative
path as part of the [INAUDIBLE].

59
00:03:16,920 --> 00:03:19,650
This is the first example;
this is a full path.

60
00:03:19,650 --> 00:03:24,030
If you're on a Mac or on a Linux system
Just run this first just to check that it

61
00:03:24,030 --> 00:03:26,050
works, then we'll talk about
the Windows side of things.

62
00:03:28,940 --> 00:03:31,060
And I'm actually getting an error
that the file isn't found, so

63
00:03:31,060 --> 00:03:33,660
I'll just confirm that
it is on my desktop.

64
00:03:33,660 --> 00:03:37,980
I'm gonna open my terminal We
probably made a mistake in the path.

65
00:03:37,980 --> 00:03:39,050
I'm gonna go to my desktop.

66
00:03:41,060 --> 00:03:42,360
That's why I put in /Home.

67
00:03:42,360 --> 00:03:45,110
It should be /Users/dev/Desktop.

68
00:03:45,110 --> 00:03:50,300
So you can see there, that's the path
on my computer to the desktop.

69
00:03:50,300 --> 00:03:53,450
So I'm too used to typing
/Home being on a Linux system.

70
00:03:53,450 --> 00:03:55,720
So I'm gonna go back and change the code.

71
00:03:55,720 --> 00:03:56,430
And paste that in.

72
00:03:56,430 --> 00:04:01,120
And put another slash there
in the name of the file.

73
00:04:01,120 --> 00:04:04,220
We'll try to run that again, and
we should be able to get it to work.

74
00:04:04,220 --> 00:04:07,630
You can see that we've got all
the data coming from the text file and

75
00:04:07,630 --> 00:04:10,370
the poem's coming out quite nicely,
so that's good.

76
00:04:10,370 --> 00:04:14,350
Now the thing to point out here is again
this is a mech path which is very similar

77
00:04:14,350 --> 00:04:14,980
to Linux path.

78
00:04:14,980 --> 00:04:16,839
You'll notice the slashes
are going forward.

79
00:04:16,839 --> 00:04:19,279
And that's the convention for
mac influence.

80
00:04:19,279 --> 00:04:21,980
But if you're running on
a Windows computer, and

81
00:04:21,980 --> 00:04:25,410
you had a username of Tim,
you'd probably do something like this.

82
00:04:25,410 --> 00:04:27,710
You'd type java, equals open.

83
00:04:27,710 --> 00:04:31,970
And it'd be something like C
colon backslash documents.

84
00:04:35,130 --> 00:04:43,600
Is settings/tim/mydocumentssample.text,
if you have it in your documents folder,

85
00:04:43,600 --> 00:04:47,860
sample.text, and The origin to
set it up for read only mode.

86
00:04:47,860 --> 00:04:50,780
Now, that doesn't always work
with the slashes with space.

87
00:04:50,780 --> 00:04:52,070
Sometimes it's bit of an issue.

88
00:04:52,070 --> 00:04:56,630
And there's also certain
characters to cause a problem,

89
00:04:56,630 --> 00:04:58,650
because if you put a /n for argument sake.

90
00:04:58,650 --> 00:05:02,700
/n is interpreted as a command and
/t as well.

91
00:05:02,700 --> 00:05:05,140
So, what it can be a good idea to do for

92
00:05:05,140 --> 00:05:09,850
your slashes is to put double slashes here
and that just makes it clear to Python.

93
00:05:09,850 --> 00:05:14,580
This is a path, and it doesn't try and
interpret the character after the slash as

94
00:05:14,580 --> 00:05:16,130
something else, so
you might do something like that.

95
00:05:16,130 --> 00:05:18,850
Even though that looks a little bit funny,

96
00:05:18,850 --> 00:05:22,240
that does make sure that that makes--you
know, really makes sure that the path is

97
00:05:22,240 --> 00:05:25,190
going to be treated as a path and
not as something else.

98
00:05:25,190 --> 00:05:25,970
So I'll comment that out and

99
00:05:25,970 --> 00:05:29,800
go back to my code because obviously I'm
on a Mac, so I'll need that to work.

100
00:05:29,800 --> 00:05:31,730
Now if I type in something, well,

101
00:05:31,730 --> 00:05:34,880
we saw the example earlier where
the path doesn't exist when we run it.

102
00:05:34,880 --> 00:05:38,610
We get an error to say,
cannot find In the file.

103
00:05:38,610 --> 00:05:40,700
So it's pretty clear that there's
a problem out in the file, and so

104
00:05:40,700 --> 00:05:42,580
it will actually crash at that point.

105
00:05:42,580 --> 00:05:43,880
And put that back in there.

106
00:05:43,880 --> 00:05:46,460
So again just a note about slashes.

107
00:05:46,460 --> 00:05:49,810
The forward slashes work equally
on a Mac and a Linux machine.

108
00:05:49,810 --> 00:05:51,070
But, they're, but the same.

109
00:05:51,070 --> 00:05:53,520
But with a Windows machine
it's back slashes.

110
00:05:53,520 --> 00:05:56,100
But you can't use back
slashes in a Linux or

111
00:05:56,100 --> 00:05:58,470
Mac path,
you need to use the forward slashes.

112
00:05:58,470 --> 00:06:01,210
So You wanna get clear based
on your operating system as to

113
00:06:01,210 --> 00:06:02,880
which way you're using your slashes.

114
00:06:02,880 --> 00:06:04,600
Obviously if I haven't
got the path in there,

115
00:06:04,600 --> 00:06:09,820
if I actually remove my path altogether,
I run that, I'm going to get an error.

116
00:06:09,820 --> 00:06:12,090
And the error is you can say
it's looking for this folder,

117
00:06:12,090 --> 00:06:17,430
user/dev/documents-programs/pauseandprogr-
ams/folderIO.

118
00:06:17,430 --> 00:06:19,050
It's looking for that folder, And

119
00:06:19,050 --> 00:06:21,810
it's actually looking for
sample.text in that folder.

120
00:06:21,810 --> 00:06:25,460
So that's why it's best to have
relativepath to get that to work.

121
00:06:25,460 --> 00:06:27,910
If you're not located
in that relative path,

122
00:06:27,910 --> 00:06:29,240
it's not going to actually work for you.

123
00:06:30,910 --> 00:06:32,860
What I can do, though, is set up a new.

124
00:06:34,090 --> 00:06:35,100
Go back into our folder.

125
00:06:36,960 --> 00:06:41,620
Now, take a copy of that and what I'm
going to do is go into the programs,

126
00:06:41,620 --> 00:06:47,000
Python, File I/O, I'm going to
paste the sample of text in there.

127
00:06:47,000 --> 00:06:49,690
So I've now got it in my path,
which you can see is

128
00:06:49,690 --> 00:06:53,380
Users\Dev\Documents\Programs\Python
Programs\File IO.

129
00:06:53,380 --> 00:06:57,340
So now if I run that, without specifying
a path, because we know the path

130
00:06:57,340 --> 00:07:01,550
by looking up here We run that,
we find that it does find it.

131
00:07:01,550 --> 00:07:06,240
So it's all a matter of choosing
the right folder to put the files in.

132
00:07:06,240 --> 00:07:08,720
You can even just specify a path in full.

133
00:07:08,720 --> 00:07:11,630
Or if you know you're in that particular
folder, you can just use the,

134
00:07:11,630 --> 00:07:15,250
or select the actual name,
without specifying the path.

135
00:07:15,250 --> 00:07:18,770
so at this point forward,
I'm not going to specify a path anymore so

136
00:07:18,770 --> 00:07:22,950
all the files that we read and write are
going to be in that particular folder so

137
00:07:22,950 --> 00:07:26,100
keep in mind that when you're working
along with this, to make sure you put

138
00:07:26,100 --> 00:07:31,470
the symbol dot text in the actual folder
where this particular program resides.

139
00:07:31,470 --> 00:07:33,820
Now, when you go online to
find Python examples, or

140
00:07:33,820 --> 00:07:38,390
you read the official documentation,
you'll come across the term Pythonic to

141
00:07:38,390 --> 00:07:42,780
describe the Python way of doing things,
compared to other programming languages.

142
00:07:42,780 --> 00:07:45,740
And I've used that word a few
times as we've been through.

143
00:07:45,740 --> 00:07:50,330
Now, this is an excellent
example of what Pythonic means.

144
00:07:50,330 --> 00:07:53,480
In most of the languages, after opening
the file, You have to check for

145
00:07:53,480 --> 00:07:55,840
the end of the file,
normally by using a while loop.

146
00:07:55,840 --> 00:07:59,120
But Python, that's to interact
through the lines of the file

147
00:07:59,120 --> 00:08:01,580
as though it were a list,
which is pretty fabulous.

148
00:08:01,580 --> 00:08:02,360
And you can, of course,

149
00:08:02,360 --> 00:08:05,040
perform another processing as
you iterate through the files.

150
00:08:05,040 --> 00:08:06,900
For example, we could check each line, and

151
00:08:06,900 --> 00:08:10,000
only print out the ones containing
the title character, for arguments' sake.

152
00:08:10,000 --> 00:08:18,790
So, we could do something like this,
if Jabberwalk, in, line dot lower.

153
00:08:18,790 --> 00:08:22,410
I'm converting it to lower case there so
we can do a case check

154
00:08:22,410 --> 00:08:25,430
against the stream that we've entered,
which is already lower case.

155
00:08:25,430 --> 00:08:29,220
Only then,
you can get a print line for this,

156
00:08:29,220 --> 00:08:34,020
also print end equals what so, so
it doesn't go into the next line.

157
00:08:34,020 --> 00:08:34,520
Run that.

158
00:08:36,390 --> 00:08:39,570
I'm not too sure what's showing an error
there, but the code is definitely valid,

159
00:08:39,570 --> 00:08:43,700
so it could be something to do with the
new version of the Python plug in perhaps,

160
00:08:43,700 --> 00:08:45,210
but that's definitely
valid code there anyway.

161
00:08:45,210 --> 00:08:48,420
So, you can see that it's working,
and it's only returning the lines

162
00:08:48,420 --> 00:08:52,920
that have got the word Jabberwalk in,
and printing them to the screen.

163
00:08:52,920 --> 00:08:56,440
Remember that string's just a sequence
of characters, so as a result,

164
00:08:56,440 --> 00:08:58,790
we can test for
the sequence general being in the lines.

165
00:08:58,790 --> 00:09:00,580
You can see that I've done a line four.

166
00:09:00,580 --> 00:09:03,730
Now, we haven't specifically covered
a string's lower method to ensure that we

167
00:09:03,730 --> 00:09:07,080
find Jabberwock whether it's in upper or
lowercase or a mix of both.

168
00:09:07,080 --> 00:09:11,660
Now, I mentioned earlier that
we're using .lower, so that way,

169
00:09:11,660 --> 00:09:16,360
we're testing whether the string
Jabberwock Text is upper or lower case.

170
00:09:16,360 --> 00:09:19,070
We're still gonna find a match because
we've typed what we're searching for

171
00:09:19,070 --> 00:09:20,060
in lowercase.

172
00:09:20,060 --> 00:09:23,230
And we're using the .ToLower method,
which is from the string class.

173
00:09:23,230 --> 00:09:26,970
and that's enabling us to do a comparison
in respective of the case that's being

174
00:09:26,970 --> 00:09:28,800
typed in for the.

175
00:09:28,800 --> 00:09:32,060
And what the n did, was it specified
that it should not automatic line

176
00:09:32,060 --> 00:09:34,130
after each line was printed,
and you saw that when I ran it.

177
00:09:35,210 --> 00:09:37,250
You didn't space it out like
the other examples we're showing.

178
00:09:37,250 --> 00:09:39,610
And the reason we're doing that
is because the value of line,

179
00:09:39,610 --> 00:09:42,550
as it's read from the file,
contains the new line character

180
00:09:42,550 --> 00:09:45,200
that is in the file itself
to move on to the new line.

181
00:09:45,200 --> 00:09:49,180
Which is why earlier on when I was
running this display, In the entire file,

182
00:09:49,180 --> 00:09:50,820
we've got double spacing.

183
00:09:50,820 --> 00:09:55,730
That's specifying n equals and basically
two single quotes keeps print from

184
00:09:55,730 --> 00:09:59,700
printing another two-line character,
which in this example is perfect.

185
00:09:59,700 --> 00:10:00,930
And finally of course on line seven,

186
00:10:00,930 --> 00:10:03,990
jabberwock close closes
the file To tidy everything up.

187
00:10:03,990 --> 00:10:04,820
Very important to do that.

188
00:10:04,820 --> 00:10:08,410
In fact, failing to close a file after
reading from it can result in subsequent

189
00:10:08,410 --> 00:10:10,690
attempts to read or
write to that file to fail.

190
00:10:10,690 --> 00:10:13,940
And if you fail to close
a file after writing to it,

191
00:10:13,940 --> 00:10:18,060
then the file itself can become corrupted
and not all data can be written to it.

192
00:10:18,060 --> 00:10:22,000
Now, with this example, there's an even
more Pythonic way to open files that,

193
00:10:22,000 --> 00:10:24,810
as well as being more concise,
also removes the risk of

194
00:10:24,810 --> 00:10:28,180
failing to close the file afterwards,
which is always a good thing.

195
00:10:28,180 --> 00:10:33,170
So, Python has a width statement that
was introduced initially in Python 2.5,

196
00:10:33,170 --> 00:10:37,450
and if an object supports a use of width,
and a file does, then width

197
00:10:37,450 --> 00:10:40,770
will tidy everything up automatically
once the object's no longer needed.

198
00:10:40,770 --> 00:10:44,950
So looking at the previous example,
let's just comment that out and

199
00:10:44,950 --> 00:10:46,410
let's top it using width.

200
00:10:46,410 --> 00:10:51,900
So with the top open, sample dot text,

201
00:10:51,900 --> 00:10:57,620
[INAUDIBLE].

202
00:10:58,640 --> 00:10:59,160
Test java.

203
00:11:01,040 --> 00:11:01,940
Full line in java.

204
00:11:01,940 --> 00:11:07,140
If in line

205
00:11:10,160 --> 00:11:16,790
print line end equals Single quotes again.

206
00:11:16,790 --> 00:11:19,340
And again, we're ignoring that there
because it's not really an error.

207
00:11:19,340 --> 00:11:21,650
It's just showing red for some reason.

208
00:11:21,650 --> 00:11:24,440
Okay, so run it, and
you can see we got the same result.

209
00:11:24,440 --> 00:11:28,140
But there's no need to specifically close
the folder this time because the width

210
00:11:28,140 --> 00:11:29,920
takes care of that for us.

211
00:11:29,920 --> 00:11:33,480
You'll notice also the way that these
open files are associated with a variable

212
00:11:33,480 --> 00:11:34,960
by using S Jabber.

213
00:11:34,960 --> 00:11:37,010
So that we can refer to
jabber in the loop, so

214
00:11:37,010 --> 00:11:40,540
you have to use the earlier myth that
if you're writing parse and to code and

215
00:11:40,540 --> 00:11:43,430
also in that case to remember to close
the file when you are done with it.

216
00:11:43,430 --> 00:11:45,210
This has got an additional advantage.

217
00:11:45,210 --> 00:11:48,160
Without using with,
if there was an error in reading the file,

218
00:11:48,160 --> 00:11:51,470
The with stopping the file
may not be closed, which,

219
00:11:51,470 --> 00:11:54,920
in a especially could mean that
it can't be deleted or moved.

220
00:11:54,920 --> 00:11:56,600
So programs often have to areas and

221
00:11:56,600 --> 00:11:59,960
ensure that things like files are closed
if an error in fact does occur.

222
00:11:59,960 --> 00:12:02,830
Now we're going to be
looking at error soon.

223
00:12:02,830 --> 00:12:06,160
But using width here means that it's
not something we need to worry about.

224
00:12:06,160 --> 00:12:08,910
If there's an error reading from the file,
then the width statement will

225
00:12:08,910 --> 00:12:12,720
the exception, and close the file before
the error terminates the program, and

226
00:12:12,720 --> 00:12:15,290
that's pretty cool that's
handling all that for us.

227
00:12:15,290 --> 00:12:16,370
So, from now on in the course,

228
00:12:16,370 --> 00:12:18,880
we're going to be using width
whenever an object supports it.

229
00:12:18,880 --> 00:12:21,090
So there are plenty of examples
to get used to using that, and

230
00:12:21,090 --> 00:12:23,530
we do suggest that you do
use it as much as possible.

231
00:12:23,530 --> 00:12:25,450
Now, before we move on to
look at different modes for

232
00:12:25,450 --> 00:12:28,110
opening a file, we'll have a look
at another couple of ways we

233
00:12:28,110 --> 00:12:32,650
could've written the first program to show
the use of read, read line and read lines.

234
00:12:32,650 --> 00:12:36,700
SO, i'm gonna close this down and
we can also change this.

235
00:12:36,700 --> 00:12:41,000
We got could've put line equals.

236
00:12:41,000 --> 00:12:44,470
And so what i'll do is what I done before

237
00:12:44,470 --> 00:12:47,560
commenting code you got the various
code you use in each lecture.

238
00:12:47,560 --> 00:12:50,520
So, we'll start again with open.

239
00:12:50,520 --> 00:12:52,710
Sample.text again.

240
00:12:54,440 --> 00:13:00,000
Read only, S jabber, and
this one we'll talk line equals jabber

241
00:13:00,000 --> 00:13:05,720
.readline while

242
00:13:05,720 --> 00:13:10,690
line, print line.

243
00:13:10,690 --> 00:13:15,090
And we talk line equals jabber.readline.

244
00:13:16,390 --> 00:13:18,840
Like so.
And if you run that

245
00:13:18,840 --> 00:13:20,750
you can see we've got
the whole poem this time.

246
00:13:20,750 --> 00:13:24,390
And notice that it has no
double spacing anymore because

247
00:13:24,390 --> 00:13:26,790
the end=' ' has removed that extra line.

248
00:13:26,790 --> 00:13:31,370
Because, of course, there was an end
line in the actual text file itself.

249
00:13:31,370 --> 00:13:32,630
And so, what we're saying is.

250
00:13:32,630 --> 00:13:38,950
By putting n="" or an empty string, we're
saying don't print that line as well,

251
00:13:38,950 --> 00:13:41,000
and that's why we're not
getting that double up anymore.

252
00:13:41,000 --> 00:13:44,340
And in terms of using red line,
you can see that we read the first line of

253
00:13:44,340 --> 00:13:49,050
the file on line 14, Into the variable
line, and if some data be read,

254
00:13:49,050 --> 00:13:51,790
in other words, the file wasn't empty,
then we enter the wallet.

255
00:13:51,790 --> 00:13:52,740
Now, if there was a problem,

256
00:13:52,740 --> 00:13:57,500
and the file was empty,
it wouldn't process from line 15 onwards.

257
00:13:57,500 --> 00:13:58,940
It wouldn't get into the wall,
in other words.

258
00:13:58,940 --> 00:14:03,070
At that point the while loop is gonna
continue until there's no lines,

259
00:14:03,070 --> 00:14:04,960
there's no additional lines to read.

260
00:14:04,960 --> 00:14:08,440
And this code is probably closer to the
way that a file is normally processed in

261
00:14:08,440 --> 00:14:11,470
other programming languages,
sort of line by line as you can see there.

262
00:14:12,610 --> 00:14:17,260
And as another example I'll just take
a copy of that and then comment that out.

263
00:14:20,940 --> 00:14:23,140
And then down here posted it again.

264
00:14:23,140 --> 00:14:26,100
There's also another example,
we could use lines.

265
00:14:26,100 --> 00:14:30,480
this case, lines equals dot read lines.

266
00:14:30,480 --> 00:14:33,740
And we can literally then just
get rid of the y altogether.

267
00:14:34,790 --> 00:14:36,960
Change the print just to print lines.

268
00:14:36,960 --> 00:14:37,810
We don't need the end any more.

269
00:14:37,810 --> 00:14:41,130
And we don't need the line either.

270
00:14:41,130 --> 00:14:44,870
So that's going to read the entire
file and return the list,

271
00:14:44,870 --> 00:14:48,540
which can then be processed
automatically as ever we would like.

272
00:14:48,540 --> 00:14:50,330
As any other list for that matter.

273
00:14:50,330 --> 00:14:52,340
So I could do that first, run it.

274
00:14:52,340 --> 00:14:54,610
You can see that's been
converted to a list.

275
00:14:54,610 --> 00:14:59,130
And then what we could also do then, is we
can put four lines using a standard list,

276
00:15:00,160 --> 00:15:02,200
loop four line in lines.

277
00:15:02,200 --> 00:15:08,200
Print line and
equals single quote, single quote.

278
00:15:08,200 --> 00:15:11,460
And if we run that,
we get a similar thing there.

279
00:15:11,460 --> 00:15:15,090
We've got going through the list
element by element effectively.

280
00:15:15,090 --> 00:15:16,210
And pretty up your typo.

281
00:15:16,210 --> 00:15:18,230
And the other thing I wanted to point out,
just run that again,

282
00:15:18,230 --> 00:15:20,260
can you see the slash end there?

283
00:15:20,260 --> 00:15:21,740
This second entry there.

284
00:15:21,740 --> 00:15:24,690
The slash end there is the sort
of end of line character.

285
00:15:24,690 --> 00:15:26,410
And that's on each element so

286
00:15:26,410 --> 00:15:29,020
that's why we need to use the end
equals single quote, single quote.

287
00:15:29,020 --> 00:15:33,920
Or entry string, in other words, to avoid
that being processed and doubling us up.

288
00:15:33,920 --> 00:15:37,710
So that's In the file itself, which you
would expect when you're creating a file.

289
00:15:37,710 --> 00:15:38,970
When you press Enter, that's what happens,

290
00:15:38,970 --> 00:15:41,660
the file normally includes
that character in there.

291
00:15:41,660 --> 00:15:42,880
That does bring up an interesting point.

292
00:15:42,880 --> 00:15:45,590
It is very useful, when you're de-bugging,
to print things like this,

293
00:15:45,590 --> 00:15:48,550
because you may not have been aware
of that, for argument's sake.

294
00:15:48,550 --> 00:15:52,650
And just seeing that that's in
the data itself that's returned

295
00:15:52,650 --> 00:15:56,760
from a read lines method can be really
useful for de-bugging purposes.

296
00:15:56,760 --> 00:16:02,150
So printing variables as a rule like this
is very useful, and you really see what

297
00:16:02,150 --> 00:16:05,440
the data is, because it may or may not
be what you had expected, and obviously,

298
00:16:05,440 --> 00:16:08,800
you would never leave that type of
output in a finished program, but for

299
00:16:08,800 --> 00:16:11,740
debugging purposes,
while you're developing it, testing it,

300
00:16:11,740 --> 00:16:14,470
debugging it,
it's really useful to see what's going on.

301
00:16:14,470 --> 00:16:17,140
So, read lines, let's close that down,

302
00:16:17,140 --> 00:16:19,800
read lines reads
the entire file in one go.

303
00:16:19,800 --> 00:16:23,790
Now this may not be a good idea for a very
large file, but can have interesting uses,

304
00:16:23,790 --> 00:16:24,980
depending on the file itself.

305
00:16:24,980 --> 00:16:27,240
So what we could have done,
and I'll just copy this again,

306
00:16:32,830 --> 00:16:37,360
down here we can put four line in lines,
and then we'll change that to a range, and

307
00:16:37,360 --> 00:16:43,780
we'll put minus one, so, and
now if we run that That might appear

308
00:16:43,780 --> 00:16:48,360
to be a bit frivolous, but it really
demonstrates what readLines is doing.

309
00:16:48,360 --> 00:16:51,260
By iterating through a reverse
slice you can see that

310
00:16:51,260 --> 00:16:52,820
we do indeed have a list of lines.

311
00:16:52,820 --> 00:16:56,590
So you contrast that with using
read in this next example and

312
00:16:56,590 --> 00:16:59,930
the instructions to print the value of
the lines variable is being removed.

313
00:16:59,930 --> 00:17:03,040
So contrast that with this code
I'm about to write here now.

314
00:17:03,040 --> 00:17:06,910
[SOUND]
So we'll do the same thing again.

315
00:17:06,910 --> 00:17:10,349
This time I'll leave that line as well.

316
00:17:10,349 --> 00:17:10,880
We'll get rid of that.

317
00:17:10,880 --> 00:17:12,010
Leave the print lines out there.

318
00:17:12,010 --> 00:17:15,380
And we'll change this to read
instead of read lines, just read.

319
00:17:17,119 --> 00:17:21,040
And run that.
And you can see now the difference.

320
00:17:21,040 --> 00:17:23,720
And the instructions will print the value
of the lines variables being removed to

321
00:17:23,720 --> 00:17:25,990
make it clear what's being reversed,
as you can see there.

322
00:17:25,990 --> 00:17:28,540
So that's been completely
reversed using .read.

323
00:17:28,540 --> 00:17:29,290
So read.

324
00:17:29,290 --> 00:17:31,820
Are really more useful
dealing with binary files.

325
00:17:31,820 --> 00:17:35,150
But this does demonstrate that the way
we've used it here to read the entire file

326
00:17:35,150 --> 00:17:36,710
into a string in one go, which is,

327
00:17:36,710 --> 00:17:40,300
of course, another way of,
of manipulating files.

328
00:17:40,300 --> 00:17:42,880
Readlines also reads the entire file.

329
00:17:42,880 --> 00:17:45,940
But gives a list of strings
rather than just a single string.

330
00:17:45,940 --> 00:17:49,040
So just to summarize before
we finish this video,

331
00:17:49,040 --> 00:17:51,930
readline reads a single line from
a file and returns a string.

332
00:17:51,930 --> 00:17:55,950
Now this is the same behavior
as we did when forline in Java,

333
00:17:55,950 --> 00:17:59,200
which is at the start, one of the first
things we did in this lecture.

334
00:17:59,200 --> 00:18:01,540
The files read one line
at a time in other words.

335
00:18:01,540 --> 00:18:04,310
The latch file this is
the recommended approach and for

336
00:18:04,310 --> 00:18:07,360
obvious reasons because it doesn't read
the entire file into memory and depending

337
00:18:07,360 --> 00:18:10,840
on your devices and the computer you may
not have a lot of memory to work with.

338
00:18:10,840 --> 00:18:11,890
Now read lines.

339
00:18:11,890 --> 00:18:14,710
Reads the entire file and
returns a list of strings.

340
00:18:14,710 --> 00:18:18,670
So, the entire file is readed into memory
which can again cause problems if you

341
00:18:18,670 --> 00:18:20,890
got very last files
occupying a lot of memory.

342
00:18:20,890 --> 00:18:23,650
And read, as we last used it on the last

343
00:18:23,650 --> 00:18:27,220
method there on the last
example Raising tar file and

344
00:18:27,220 --> 00:18:30,400
if it's a text file returns a string
containing the contents of the file.

345
00:18:30,400 --> 00:18:33,240
So read can also take
an optional parameter

346
00:18:33,240 --> 00:18:36,180
specifying how much data to read,
and we'll see that in use later.

347
00:18:36,180 --> 00:18:38,920
So that's it,
I'm going to finish the video here now.

348
00:18:38,920 --> 00:18:42,260
In the next video we're going to start
talking about writing to a text file.

349
00:18:42,260 --> 00:18:43,450
See you in that next video.

