1
00:00:08,000 --> 00:00:13,905
Welcome back. Writing to a file is pretty similar to a reading from a file.

2
00:00:13,905 --> 00:00:18,225
You still have to open a file object based on a name for the file.

3
00:00:18,225 --> 00:00:22,110
But instead of reading from the file object, you'll write to it.

4
00:00:22,110 --> 00:00:24,285
Let's see an example.

5
00:00:24,285 --> 00:00:28,590
Here we're printing out the squares of all the numbers from zero up to,

6
00:00:28,590 --> 00:00:30,765
but not including 13.

7
00:00:30,765 --> 00:00:35,100
If I run it, we'll see we get zero times zero,

8
00:00:35,100 --> 00:00:36,420
and one times one is one,

9
00:00:36,420 --> 00:00:40,090
and then four, three times three is nine and so on.

10
00:00:40,090 --> 00:00:43,730
Now, suppose instead of writing those to the output window,

11
00:00:43,730 --> 00:00:48,210
we wanted to write them to a file where they would be permanently stored.

12
00:00:48,220 --> 00:00:52,380
Well, let's start coding that up.

13
00:00:52,550 --> 00:00:57,560
A normal little template for reading or writing from

14
00:00:57,560 --> 00:01:03,465
a file is that we have some file object equals open,

15
00:01:03,465 --> 00:01:05,415
add some file name,

16
00:01:05,415 --> 00:01:08,050
I'll call it squares.text,

17
00:01:08,050 --> 00:01:12,670
and now we have to say writing instead of reading.

18
00:01:12,670 --> 00:01:15,950
Whenever I open a file object like that,

19
00:01:15,950 --> 00:01:19,520
I tend to forget that I need to close it,

20
00:01:19,520 --> 00:01:23,190
so I'll just put the close in right away.

21
00:01:23,800 --> 00:01:26,970
Now, if I run this,

22
00:01:27,170 --> 00:01:31,670
nothing different happens because I'm still printing to the output window.

23
00:01:31,670 --> 00:01:34,655
So, instead of printing to the output window,

24
00:01:34,655 --> 00:01:37,220
I want to write to a file.

25
00:01:37,220 --> 00:01:45,265
So, I'm going to say, file_ obj.write instead of having a print statement,

26
00:01:45,265 --> 00:01:50,490
and I'm going to have to turn that number zero,

27
00:01:50,490 --> 00:01:54,560
one, four, or whatever into a string in order to be able to write it.

28
00:01:54,560 --> 00:01:56,810
The print function is pretty forgiving.

29
00:01:56,810 --> 00:02:00,170
We could give it a number or a string and it would figure it out but here,

30
00:02:00,170 --> 00:02:02,730
we have to actually give it a string.

31
00:02:02,930 --> 00:02:05,080
So, if I do this,

32
00:02:05,080 --> 00:02:09,980
I will now have at the end in my file object all those numbers zero,

33
00:02:09,980 --> 00:02:13,260
one, four, nine, and so on.

34
00:02:14,510 --> 00:02:18,410
Now, we have a little simulator for

35
00:02:18,410 --> 00:02:23,480
written files just as we have a simulator for reading from files.

36
00:02:23,480 --> 00:02:25,250
In Runestone as you recall,

37
00:02:25,250 --> 00:02:31,040
we can't read or write files from the local file system for security reasons.

38
00:02:31,040 --> 00:02:36,500
So, we have built in disability to read a few files that are built into each page

39
00:02:36,500 --> 00:02:42,610
and we can write files which will be available just until the page gets reloaded.

40
00:02:42,610 --> 00:02:47,040
So, we have this file called squares.text and here's the output.

41
00:02:47,040 --> 00:02:50,420
Now, that output may be a little different from what you were

42
00:02:50,420 --> 00:02:54,530
expecting because we have zero,

43
00:02:54,530 --> 00:02:58,365
one, four, nine, and so on,

44
00:02:58,365 --> 00:03:00,680
it's not nice like it was in the output window.

45
00:03:00,680 --> 00:03:01,760
We don't have zero,

46
00:03:01,760 --> 00:03:04,975
one, four, each on its own line.

47
00:03:04,975 --> 00:03:09,170
The reason for that difference is that when you call

48
00:03:09,170 --> 00:03:16,295
print and you give it a string like four,

49
00:03:16,295 --> 00:03:23,400
you'll automatically get four and a new line in the output window.

50
00:03:23,540 --> 00:03:27,990
When we call.write, we just get the contents.

51
00:03:27,990 --> 00:03:29,595
So, we just get the four,

52
00:03:29,595 --> 00:03:31,695
but we don't get a new line character.

53
00:03:31,695 --> 00:03:36,340
You have to decide for yourself when you want a new line.

54
00:03:36,340 --> 00:03:40,820
So, what I'm going do is after I've written each square,

55
00:03:40,820 --> 00:03:45,135
I'm also going to explicitly write a new line,

56
00:03:45,135 --> 00:03:48,010
the backslash n character.

57
00:03:48,260 --> 00:03:50,670
Now, if I save and run it,

58
00:03:50,670 --> 00:03:54,950
we'll see something that looks a little nicer in the data file.

59
00:03:54,950 --> 00:04:04,150
Now we've got all the values each one on its own line.

60
00:04:05,330 --> 00:04:10,470
Of course, we could combine these onto a single line.

61
00:04:10,470 --> 00:04:17,055
We could have the string of square plus \n all on one line,

62
00:04:17,055 --> 00:04:19,720
that would work just as well.

63
00:04:19,790 --> 00:04:23,690
Sometimes, especially for students who are just learning,

64
00:04:23,690 --> 00:04:26,300
I like to make the new line character be

65
00:04:26,300 --> 00:04:29,300
its own line because it's a real reminder that without write,

66
00:04:29,300 --> 00:04:32,195
you have to create that new line character

67
00:04:32,195 --> 00:04:36,830
explicitly unlike with the print function where it does it for you.

68
00:04:36,830 --> 00:04:39,520
So, that works just the same.

69
00:04:39,520 --> 00:04:45,784
Now, when we have a file for the duration of this page being displayed,

70
00:04:45,784 --> 00:04:47,765
that file is available.

71
00:04:47,765 --> 00:04:52,045
So, I could read it.

72
00:04:52,045 --> 00:04:55,110
I can read that file, it's called squares.text.

73
00:04:55,110 --> 00:05:04,310
So, new file object equals open of squares.text and this time,

74
00:05:04,310 --> 00:05:13,230
I'm opening it for reading and let's just print out let's say,

75
00:05:13,230 --> 00:05:15,910
the first 10 characters.

76
00:05:18,920 --> 00:05:21,750
The.read gets me all the characters.

77
00:05:21,750 --> 00:05:23,970
If I just want the first 10 characters,

78
00:05:23,970 --> 00:05:27,980
I'll do that, and I will see something in the output window.

79
00:05:27,980 --> 00:05:30,750
Let me clear all of my markings,

80
00:05:37,760 --> 00:05:44,895
and now you see we've got the first 10 characters showing up in the output window.

81
00:05:44,895 --> 00:05:48,850
The first character is a zero and then there's a second character for the new line.

82
00:05:48,850 --> 00:05:52,090
So, that's two characters 3, 4, 5,

83
00:05:52,090 --> 00:05:55,785
6, 7, 8, 9, and 10.

84
00:05:55,785 --> 00:06:00,875
If I had asked for just the first nine characters,

85
00:06:00,875 --> 00:06:05,120
I would have up to the one without the six,

86
00:06:07,610 --> 00:06:10,125
and sure enough, there it is.

87
00:06:10,125 --> 00:06:13,230
By the way, this file really is

88
00:06:13,230 --> 00:06:20,365
there even if I don't recreate it each time with the code.

89
00:06:20,365 --> 00:06:23,455
I could now just read it if I wanted to.

90
00:06:23,455 --> 00:06:27,070
So, let me get the first 14 characters, let's say.

91
00:06:27,070 --> 00:06:30,050
Let's say you'll see that we get something different,

92
00:06:31,220 --> 00:06:36,430
and sure enough, we get the 25 in addition.

93
00:06:38,900 --> 00:06:43,040
As I said, we've simulated the creation of this file,

94
00:06:43,040 --> 00:06:46,285
it's there until we reload the page.

95
00:06:46,285 --> 00:06:48,130
Let me just demonstrate that for you.

96
00:06:48,130 --> 00:06:51,905
If I reload the page but try to keep this code,

97
00:06:51,905 --> 00:06:53,460
we're going to have a problem,

98
00:06:53,460 --> 00:06:55,990
it won't be able to find the file.

99
00:06:58,430 --> 00:07:02,480
So, if I try to run the same code again,

100
00:07:03,380 --> 00:07:07,765
it says that there is no such file or directory squares.text.

101
00:07:07,765 --> 00:07:11,260
So, it can't open it because it hasn't been created.

102
00:07:11,260 --> 00:07:13,270
That's how we write to files.

103
00:07:13,270 --> 00:07:15,460
It structurally similar to reading.

104
00:07:15,460 --> 00:07:17,435
We open the file,

105
00:07:17,435 --> 00:07:19,670
just as we do for reading,

106
00:07:19,670 --> 00:07:20,980
but when we're writing,

107
00:07:20,980 --> 00:07:24,580
we do with a "w" instead of "r".

108
00:07:24,580 --> 00:07:31,205
We call.write as needed but if we want a new line,

109
00:07:31,205 --> 00:07:34,885
we have to explicitly write the backslash n character.

110
00:07:34,885 --> 00:07:37,570
Then, we have to close the file.

111
00:07:37,570 --> 00:07:40,060
It's especially important for writing because otherwise,

112
00:07:40,060 --> 00:07:42,850
the contents might not be fully written and you might

113
00:07:42,850 --> 00:07:46,940
lose some of them. See you next time.