1
00:00:05,420 --> 00:00:09,520
So in the previous two videos, we learned
about reading and writing to text files. | www.downloadly.ir

2
00:00:09,520 --> 00:00:11,950
So they're the two modes that you
would use, probably, fairly often.

3
00:00:11,950 --> 00:00:16,390
And we can also append to a text file
by using an A as the mode for append.

4
00:00:16,390 --> 00:00:20,320
Now, opening a text file in append
mode causes data to be written to

5
00:00:20,320 --> 00:00:21,590
the end of the file.

6
00:00:21,590 --> 00:00:24,000
Without overriding the existing contents.

7
00:00:24,000 --> 00:00:27,100
In fact these three modes are short for
RT, WT, and AT.

8
00:00:27,100 --> 00:00:28,670
Or TR, TW, and TA.

9
00:00:28,670 --> 00:00:32,090
The letters can be specified
either way around.

10
00:00:32,090 --> 00:00:35,050
So the T stands for
text mode which is the default.

11
00:00:35,050 --> 00:00:36,920
You don't ask for text or binary.

12
00:00:36,920 --> 00:00:40,350
Now there's an interesting
link here that you can go to

13
00:00:40,350 --> 00:00:42,170
to find out more about
the file opening mode.

14
00:00:42,170 --> 00:00:43,910
So I'm just going to
open that up to show you.

15
00:00:43,910 --> 00:00:45,640
And this link will be in
the resources section.

16
00:00:47,540 --> 00:00:52,990
And you can see here the modes on the
screen, so we've got various modes there.

17
00:00:52,990 --> 00:00:55,930
And you can see if you don't specify mode,

18
00:00:55,930 --> 00:01:00,050
the default is auto is set to RT to
open a file, a text file, for reading.

19
00:01:00,050 --> 00:01:03,160
And as it turns out, we'll be looking at
a binary file next in the next video,

20
00:01:03,160 --> 00:01:08,470
but it's also possible to open a file for
both reading and writing by using plus.

21
00:01:08,470 --> 00:01:14,040
But that doesn't replace r or w so
you have to use r plus or w plus or such.

22
00:01:14,040 --> 00:01:15,390
Now mode x on the screen there,

23
00:01:15,390 --> 00:01:19,060
it can be useful if you expect that file
shouldn't already exist and so it will

24
00:01:19,060 --> 00:01:23,020
raise an error if you try to create a file
with the same name as the existing one.

25
00:01:23,020 --> 00:01:26,870
And there can be some situations and
circumstances where that can be useful.

26
00:01:26,870 --> 00:01:29,040
So before we move on
now to binary files and

27
00:01:29,040 --> 00:01:31,810
better ways to store variables,
it's time for a challenge.

28
00:01:31,810 --> 00:01:32,850
So let's go back to our code.

29
00:01:32,850 --> 00:01:36,090
And we're gonna create a new file,
new path and file,

30
00:01:36,090 --> 00:01:37,700
we're just gonna call it challenge.

31
00:01:37,700 --> 00:01:41,810
So the challenge is
going to be as follows.

32
00:01:41,810 --> 00:01:46,260
And what I'm going to do is I'm just
going to paste the code in Okay so

33
00:01:46,260 --> 00:01:48,170
there is our challenge.

34
00:01:48,170 --> 00:01:52,560
So we're going to write a program
to append a times table to our

35
00:01:53,680 --> 00:01:54,450
in sample dot text.

36
00:01:54,450 --> 00:01:57,120
So we want the tables from two
to 12 similar to the output

37
00:01:57,120 --> 00:02:01,900
from the lecture which was in
section six of this course.

38
00:02:01,900 --> 00:02:04,800
So the first column of numbers
should be right justified.

39
00:02:04,800 --> 00:02:07,470
As an example, the two times table should
look like you can see there below.

40
00:02:07,470 --> 00:02:09,620
You can see how that's
formatted going from 1 to 12.

41
00:02:09,620 --> 00:02:12,450
So again what you're going to want to
do is write a program that's going to

42
00:02:12,450 --> 00:02:16,910
append these tables, these time tables,
to the Jabberwocky palm and

43
00:02:16,910 --> 00:02:20,000
sample dot text which you've
worked within the last few videos.

44
00:02:20,000 --> 00:02:23,510
So pause the video now, go away, and
see if you can come up with that.

45
00:02:23,510 --> 00:02:27,840
Based on what I talked to you about very
briefly in this video about a pending.

46
00:02:27,840 --> 00:02:31,700
And you can also refer back to that
documentation page that I showed you

47
00:02:31,700 --> 00:02:34,400
to find out the appropriate modes and
so forth to use.

48
00:02:34,400 --> 00:02:36,950
So have a go at that and
when you're ready to come back and

49
00:02:36,950 --> 00:02:40,610
see a solution or our solution to it
come back and I'll show you that.

50
00:02:44,600 --> 00:02:45,520
Okay so did you get on?

51
00:02:45,520 --> 00:02:47,980
Hopefully you managed to get it to work.

52
00:02:47,980 --> 00:02:50,490
So here's the solution
now we came up with.

53
00:02:50,490 --> 00:02:55,080
So we're going to start by opening
the file so it's with open,

54
00:02:55,080 --> 00:02:59,700
and of course it's going to be sample.txt.

55
00:02:59,700 --> 00:03:01,900
Whoops, I now need to specify the mode,
which is a for

56
00:03:01,900 --> 00:03:04,820
pending I'm going to call it tables.

57
00:03:06,390 --> 00:03:10,790
And then I'm going to go 4 I in range,
to 215.

58
00:03:10,790 --> 00:03:16,400
And then I'm going to put for
J, for J in range 113.

59
00:03:16,400 --> 00:03:24,150
And then I'm going to print
using replacement field syntax

60
00:03:28,650 --> 00:03:36,440
Two like so times is two and

61
00:03:36,440 --> 00:03:43,450
we use a .format which is gonna be i j and
of course i times j.

62
00:03:43,450 --> 00:03:48,540
And then in the end of it files = tables.

63
00:03:48,540 --> 00:03:51,940
And then we finish it off with a print

64
00:03:55,170 --> 00:04:00,890
times 20, files = tables.

65
00:04:00,890 --> 00:04:02,830
File yet, that should be file.

66
00:04:03,970 --> 00:04:08,210
That should be it, so just confirming
that our sample got text at the moment

67
00:04:08,210 --> 00:04:12,020
it's just got the Jabberwocky poem so
I'll just close it down again.

68
00:04:12,020 --> 00:04:16,440
And if we now run this we don't
get anything output directly

69
00:04:16,440 --> 00:04:19,370
because we've written everything
to a file not the console.

70
00:04:19,370 --> 00:04:21,550
But now if I check my sample dot text.

71
00:04:21,550 --> 00:04:27,260
You can see we've got the times tables,
correctly showing in the end of that file.

72
00:04:27,260 --> 00:04:29,290
So we've correctly appended to that file.

73
00:04:29,290 --> 00:04:32,530
Now note that each time that we've
run this program, I run it again.

74
00:04:32,530 --> 00:04:37,510
And I'll just close it down to open it up,
to make it easier.

75
00:04:39,850 --> 00:04:46,120
And, we've got the data repeated twice so
its actually come back and done it again.

76
00:04:46,120 --> 00:04:50,190
Because pent output H time we run the
program in effect starting at the end of

77
00:04:50,190 --> 00:04:53,120
the file, and if you wanted
to create a new file instead,

78
00:04:53,120 --> 00:04:56,830
obviously, head back to our code and
change the A to a W.

79
00:04:56,830 --> 00:04:59,620
And, that would create
the new file each time.

80
00:04:59,620 --> 00:05:02,890
So, for example, And

81
00:05:02,890 --> 00:05:05,500
I'll just call name something else so
we don't overwrite the original file.

82
00:05:05,500 --> 00:05:06,120
And you'll run that.

83
00:05:07,470 --> 00:05:12,510
You'll see sample two appear
there on the left hand side.

84
00:05:12,510 --> 00:05:13,820
There's our code.

85
00:05:13,820 --> 00:05:18,820
143 lines there, and
if we run it again Just to confirm,

86
00:05:18,820 --> 00:05:23,060
I've still got 143, so it's obviously
creating the file each time.

87
00:05:23,060 --> 00:05:27,090
So that's it, that's the end of
these three videos on reading and

88
00:05:27,090 --> 00:05:28,520
writing text files.

89
00:05:28,520 --> 00:05:31,300
In the next video, we're going to
start looking at binary files,

90
00:05:31,300 --> 00:05:34,280
which is a different mechanism for
saving different types of data.

91
00:05:34,280 --> 00:05:35,490
So, see you int hat next video. | www.downloadly.ir

