1
00:00:01,710 --> 00:00:08,100
I don't know if you have wondering about it, but we have a big issue with our program.

2
00:00:08,970 --> 00:00:17,160
The issue is that the user enters these to do items, but then if you close the program, those data

3
00:00:17,160 --> 00:00:18,330
are lost.

4
00:00:21,630 --> 00:00:26,820
So if I run the program again and I say show it shows nothing.

5
00:00:28,400 --> 00:00:35,900
And that is, of course, the expected behavior of the program, because we are trying to save the user

6
00:00:35,900 --> 00:00:39,200
data, the to do items in variables.

7
00:00:39,230 --> 00:00:40,700
The problem is that.

8
00:00:41,870 --> 00:00:47,900
When the program ends, its execution, variable values are discarded.

9
00:00:49,140 --> 00:00:56,970
Because they are simply saved in temporary memory on your computer while the program is up and running.

10
00:00:57,720 --> 00:01:00,480
So we need to figure out a better solution.

11
00:01:00,690 --> 00:01:06,660
The solution is to get the user input and store it via Python.

12
00:01:07,500 --> 00:01:11,550
In an external file on disk.

13
00:01:13,170 --> 00:01:15,480
And that is what you're going to learn in this video.

14
00:01:15,480 --> 00:01:21,540
So you're going to learn how to store data in text files.

15
00:01:21,570 --> 00:01:25,530
Now, there are different solutions to store user data.

16
00:01:25,710 --> 00:01:30,900
So one is text files, which is what we're going to use in this lecture.

17
00:01:31,110 --> 00:01:36,810
Text files is suggested to be used because it's a simple solution and it works very well with simple

18
00:01:36,810 --> 00:01:37,270
data.

19
00:01:37,290 --> 00:01:41,730
So in our case, we simply have these sentences.

20
00:01:42,390 --> 00:01:51,060
Now, if you had more complex data, you might want to use CSV format, so CSV files or JSON, and if

21
00:01:51,060 --> 00:01:59,460
you have even more complex data where there is more user interaction with the data, then you might

22
00:01:59,460 --> 00:02:03,590
want to go with the ultimate solution, which is SQL databases.

23
00:02:03,600 --> 00:02:08,400
So we're going to cover all these technologies in the course.

24
00:02:08,430 --> 00:02:11,009
Now let's focus on text files.

25
00:02:12,270 --> 00:02:16,800
So first of all, I would start by creating an empty text file.

26
00:02:16,860 --> 00:02:21,630
So to do that, you can right click over your project directory here.

27
00:02:24,300 --> 00:02:32,790
And go to new and go to file and give a name to the file, such as to do the pt x t, So that is a text

28
00:02:32,790 --> 00:02:33,300
file.

29
00:02:33,720 --> 00:02:39,420
Make sure that the text file is created in the same directory with your main AP file.

30
00:02:39,930 --> 00:02:41,910
So these are in the same folder.

31
00:02:44,520 --> 00:02:52,620
Then go to Maine that p y, and let's implement the ADD case first.

32
00:02:53,490 --> 00:03:00,900
So currently we have an empty list in here and then the user enters a to do item and we append that

33
00:03:00,900 --> 00:03:02,220
to the item to the list.

34
00:03:02,220 --> 00:03:06,090
So to do is update it at this point with a user.

35
00:03:07,040 --> 00:03:08,120
To the item.

36
00:03:09,980 --> 00:03:19,610
Now, we will keep these two lines as they are, but then we want to store the list items in a text

37
00:03:19,610 --> 00:03:20,120
file.

38
00:03:20,150 --> 00:03:21,200
To do that.

39
00:03:22,210 --> 00:03:31,450
You first want to create a file variable and this file variable, you want to store a file object.

40
00:03:31,840 --> 00:03:38,590
The file object is returned by the open function.

41
00:03:39,190 --> 00:03:47,010
So Open produces this certain type of object, a file object, just like we have string objects.

42
00:03:47,200 --> 00:03:49,390
That is a string object.

43
00:03:51,820 --> 00:03:53,390
That is a list object.

44
00:03:53,410 --> 00:03:55,210
We also have file objects.

45
00:03:55,270 --> 00:04:01,240
So these are designed to interact with files on disk with that file.

46
00:04:01,240 --> 00:04:03,280
So to interact with that file.

47
00:04:03,310 --> 00:04:09,880
This method requires the path to that file to dos the text.

48
00:04:11,080 --> 00:04:15,870
And then there is a second argument you want to specify, which is.

49
00:04:17,200 --> 00:04:18,970
A string again, It could be.

50
00:04:18,970 --> 00:04:20,440
W it could be.

51
00:04:20,440 --> 00:04:27,180
Or in this case it's w w means right and R means right.

52
00:04:27,190 --> 00:04:30,430
So if you want to write something in that file.

53
00:04:32,430 --> 00:04:33,440
Via Python.

54
00:04:33,450 --> 00:04:34,780
Then you write.

55
00:04:34,800 --> 00:04:39,010
W if you want to read the content of that file.

56
00:04:39,030 --> 00:04:45,810
So if you want to print out the content in via Python in the command line here, you want to read that

57
00:04:45,810 --> 00:04:50,880
file, so you extract the text from that file, store it in a python string and print it out.

58
00:04:51,600 --> 00:04:53,490
In this case, we want to write.

59
00:04:55,970 --> 00:04:57,590
So that will create a foil object.

60
00:04:57,590 --> 00:05:01,250
And then in that file object, we write lines.

61
00:05:01,250 --> 00:05:07,850
So write lines is a method available for file objects.

62
00:05:10,780 --> 00:05:16,090
And this right line's method gets as arguments a list.

63
00:05:17,020 --> 00:05:23,350
So our list is to deduce therefore the items of this list will be written into those the text.

64
00:05:23,560 --> 00:05:25,210
So let's try this.

65
00:05:25,930 --> 00:05:27,430
I'm going to run the script.

66
00:05:30,830 --> 00:05:35,780
Press add to indicate that you want to add an item clean.

67
00:05:39,240 --> 00:05:47,430
At again through and then exit to exit the program, press enter, and then we want to check to do that

68
00:05:47,430 --> 00:05:48,090
text.

69
00:05:49,580 --> 00:05:55,580
If you don't see anything, try to read, click here and go to real load from disk and that will refresh

70
00:05:55,580 --> 00:06:00,760
the content over to those that text file in your patch already.

71
00:06:01,070 --> 00:06:08,810
So these are the data we entered, not quite as you were expecting, probably because you see that these

72
00:06:08,810 --> 00:06:11,390
are merged together.

73
00:06:14,320 --> 00:06:16,180
There is an easy solution for that, though.

74
00:06:16,480 --> 00:06:18,130
You can simply.

75
00:06:20,730 --> 00:06:26,040
Add a backslash and character after this.

76
00:06:26,550 --> 00:06:31,260
So that part contains the user input.

77
00:06:31,260 --> 00:06:33,360
So clean, for example.

78
00:06:33,370 --> 00:06:39,690
So if you say clean plus that what you get is I can show you that in the console.

79
00:06:39,690 --> 00:06:45,270
So basically we're doing this clean plus backslash n.

80
00:06:48,180 --> 00:06:52,170
So that merges the two strings.

81
00:06:52,350 --> 00:06:53,940
But if you use print.

82
00:06:55,000 --> 00:06:55,810
And you do it again.

83
00:06:55,810 --> 00:06:56,530
Clean.

84
00:06:57,400 --> 00:06:58,300
Plus.

85
00:07:00,400 --> 00:07:08,440
RN and you press enter, then what you get is clean plus a brake line.

86
00:07:08,830 --> 00:07:12,790
So n is a backslash, n is actually a brake line.

87
00:07:14,030 --> 00:07:23,360
We didn't see the brake line here because if you just do clean plus backslash and python will print

88
00:07:23,360 --> 00:07:26,270
out a very raw representation of the string.

89
00:07:26,270 --> 00:07:31,430
So it it does not recognize backslash n as an actual brake line.

90
00:07:32,670 --> 00:07:36,330
But print is designed to convert that symbol.

91
00:07:36,330 --> 00:07:39,200
That is a special symbol, special string.

92
00:07:39,210 --> 00:07:42,150
It converts them that to a break line.

93
00:07:42,810 --> 00:07:45,930
So let's go back to our script.

94
00:07:46,530 --> 00:07:47,910
And here we are.

95
00:07:47,980 --> 00:07:48,660
This again.

96
00:07:51,880 --> 00:07:52,630
Add.

97
00:07:54,720 --> 00:08:01,410
Let's try to prepare this time and add again.

98
00:08:01,440 --> 00:08:04,320
Shower exit.

99
00:08:05,720 --> 00:08:06,230
Hmm.

100
00:08:06,350 --> 00:08:07,760
Let's check to do's.

101
00:08:08,180 --> 00:08:11,000
So prepare and shower.

102
00:08:11,600 --> 00:08:13,220
So now it is working.

103
00:08:13,220 --> 00:08:14,900
Well, these are.

104
00:08:15,780 --> 00:08:16,910
Each in one line.

105
00:08:16,920 --> 00:08:21,900
But there is a problem if you've noticed it, where it's clean and through.

106
00:08:22,260 --> 00:08:24,180
So they have disappeared.

107
00:08:24,750 --> 00:08:25,920
Why is that?

108
00:08:26,110 --> 00:08:27,990
I'll I'll explain that to you.

109
00:08:28,620 --> 00:08:39,750
What happens is that this line here, what it does is if it finds that file in the directory here,

110
00:08:40,620 --> 00:08:44,049
that's methods will override that file.

111
00:08:44,070 --> 00:08:48,090
So basically, it creates a new file with that name.

112
00:08:48,510 --> 00:08:50,940
Then it writes.

113
00:08:52,720 --> 00:08:54,910
The lists in that file.

114
00:08:55,390 --> 00:09:03,540
So if there were other items, other texts in that file, it will be overwritten by these new items.

115
00:09:03,550 --> 00:09:06,250
In this case, prepare and shower.

116
00:09:07,180 --> 00:09:12,370
So this is not the expected behavior we want for the program.

117
00:09:12,370 --> 00:09:21,140
We want to keep the existing to do items and be able to add other items under those two.

118
00:09:21,160 --> 00:09:23,170
So, for example, now we have these two.

119
00:09:23,200 --> 00:09:31,240
If the user enters code in the command line, code should be written in here.

120
00:09:32,080 --> 00:09:34,630
So let's go back and fix this.

121
00:09:35,560 --> 00:09:46,870
The way to fix this is to first get an item from the user and then create a list, a python list.

122
00:09:46,870 --> 00:09:51,700
And this list is going to contain the existing items from the text file.

123
00:09:53,040 --> 00:09:55,830
So first of all, we need to delete that list.

124
00:09:57,000 --> 00:09:59,430
We don't need that list anymore.

125
00:10:01,190 --> 00:10:09,920
So what we want to do is we get the item from the user and then we say file is equal to open.

126
00:10:11,930 --> 00:10:13,970
To dos the text.

127
00:10:15,760 --> 00:10:17,260
Ah, this time.

128
00:10:17,260 --> 00:10:20,380
So we open the file in red mode.

129
00:10:20,470 --> 00:10:24,730
We want to read what is inside that to do that txt file.

130
00:10:27,560 --> 00:10:33,200
And we create a variable to dos, which is equal to file that reads lines.

131
00:10:33,680 --> 00:10:35,810
So that is the equivalent of this.

132
00:10:35,810 --> 00:10:37,100
But to read.

133
00:10:38,100 --> 00:10:46,770
So it will read to the content that is in the file and it will create a list with that content.

134
00:10:46,800 --> 00:10:50,430
Each line will be an item of that list.

135
00:10:55,590 --> 00:11:05,880
So at this point, we have a list with two items and let me make a break line and then we append.

136
00:11:08,250 --> 00:11:11,100
The user's item in that list.

137
00:11:11,100 --> 00:11:13,260
So if the user enters.

138
00:11:14,190 --> 00:11:20,250
Clean than the lists at this point will be something like prepare.

139
00:11:21,890 --> 00:11:22,630
What else?

140
00:11:22,640 --> 00:11:26,240
Shower and clean.

141
00:11:27,360 --> 00:11:29,700
The item that the user just entered.

142
00:11:29,700 --> 00:11:31,050
So something like that.

143
00:11:31,740 --> 00:11:33,000
And then.

144
00:11:34,580 --> 00:11:37,700
Let me break this as well.

145
00:11:37,760 --> 00:11:40,220
And in this part, we.

146
00:11:42,850 --> 00:11:44,770
Creates a new file.

147
00:11:46,520 --> 00:11:49,640
So we completely overwrite the existing file.

148
00:11:49,640 --> 00:11:55,790
And in that new file we write that list which contains three items now.

149
00:11:58,200 --> 00:11:59,460
So let's try this.

150
00:12:01,960 --> 00:12:02,740
At.

151
00:12:05,880 --> 00:12:07,710
Play at.

152
00:12:09,650 --> 00:12:10,400
Fix.

153
00:12:12,360 --> 00:12:18,960
And exit press answer go to to do the text and that's the output.

154
00:12:18,960 --> 00:12:21,590
So we had to prepare and shower earlier.

155
00:12:21,600 --> 00:12:23,550
Now we have play and fix.

156
00:12:25,590 --> 00:12:26,970
So that's code.

157
00:12:29,240 --> 00:12:30,860
Make sure that.

158
00:12:32,560 --> 00:12:36,130
From the user to the items are stored in a text file.

159
00:12:40,150 --> 00:12:44,500
Before we close this video, though, there's something we need to add.

160
00:12:45,980 --> 00:12:54,230
So once you open a file object here and you have finished working with that file object, then you need

161
00:12:54,230 --> 00:12:55,280
to close it.

162
00:12:57,140 --> 00:12:59,870
Things will work even without closing it.

163
00:12:59,870 --> 00:13:02,720
But sometimes.

164
00:13:04,660 --> 00:13:08,380
Different parts of the codes can interact with each other in a way that.

165
00:13:10,380 --> 00:13:14,130
They will change the contents of your file if you have not closed it.

166
00:13:14,520 --> 00:13:18,990
So it's good that once you are done with the file, just close it.

167
00:13:18,990 --> 00:13:22,100
And the same goes for this other one here.

168
00:13:22,110 --> 00:13:22,860
Close.

169
00:13:25,600 --> 00:13:29,400
So you open it here, you close it here, you open it here.

170
00:13:29,410 --> 00:13:30,610
Finish what you do.

171
00:13:30,640 --> 00:13:31,540
You close it here.

172
00:13:31,570 --> 00:13:34,750
Now, this is just a variable here.

173
00:13:34,750 --> 00:13:36,790
You could also have another variable.

174
00:13:36,790 --> 00:13:37,950
Whatever here.

175
00:13:37,960 --> 00:13:39,490
So that would work.

176
00:13:39,490 --> 00:13:42,220
File whatever, file whatever that close.

177
00:13:42,640 --> 00:13:48,460
So it doesn't matter that we happen to have the same variable names in here.

178
00:13:51,000 --> 00:13:52,020
And that's about it.

179
00:13:52,050 --> 00:13:57,780
As a summary, let me open the Python console and show you what we just did.

180
00:13:57,960 --> 00:14:06,360
So we used the open function, and this open function creates a file, whatever.

181
00:14:06,360 --> 00:14:09,900
For example, the txt that is a path to all of the file.

182
00:14:10,050 --> 00:14:12,840
With W you create a new file.

183
00:14:13,350 --> 00:14:21,660
If the file is existing, it will be overwritten with an empty file and then you can write lines to

184
00:14:21,660 --> 00:14:24,810
that file by providing a list of lines.

185
00:14:24,830 --> 00:14:25,710
Line one.

186
00:14:27,120 --> 00:14:28,080
Line two.

187
00:14:28,290 --> 00:14:29,530
You execute that.

188
00:14:29,550 --> 00:14:34,980
Don't forget to close the file using the closed methods and a new directory here.

189
00:14:35,400 --> 00:14:41,520
If you do a reload from disk, for example, you should see whatever the text.

190
00:14:41,550 --> 00:14:43,110
So those are the lines.

191
00:14:45,640 --> 00:14:49,600
So again, you can override that file by opening it again.

192
00:14:50,990 --> 00:14:52,040
Right lines.

193
00:14:52,250 --> 00:14:57,770
And if you want to have brake lines between the lines at backslash.

194
00:15:00,120 --> 00:15:01,260
Inside each string.

195
00:15:01,260 --> 00:15:02,880
So that's one string.

196
00:15:03,300 --> 00:15:04,780
That's the other string.

197
00:15:04,800 --> 00:15:07,140
Backslash n press enter.

198
00:15:07,140 --> 00:15:08,610
File that close.

199
00:15:09,720 --> 00:15:15,660
Reloads the file on par charm and you should see this new output.

200
00:15:17,320 --> 00:15:22,840
It is also possible to write texts in a file, so hear the input.

201
00:15:22,840 --> 00:15:24,440
The argument is a list.

202
00:15:24,460 --> 00:15:27,430
How can you write a simple string?

203
00:15:27,880 --> 00:15:30,130
Well, that is also possible.

204
00:15:30,430 --> 00:15:41,440
Again, we use the open function and let's say whatever does whatever to the text in write mode.

205
00:15:41,440 --> 00:15:43,000
So the code is the same.

206
00:15:43,000 --> 00:15:50,290
But then instead of write lines, you use write, and instead of passing a list, you pass simply text.

207
00:15:50,410 --> 00:15:54,550
Hey, this is just text here and there.

208
00:15:54,670 --> 00:16:06,790
You can provide backslash and characters here as well and press enter and file dots close.

209
00:16:06,790 --> 00:16:08,280
It closes the file.

210
00:16:08,290 --> 00:16:11,380
This is the number of bytes written in that file.

211
00:16:12,520 --> 00:16:16,990
Just for your information, file close and to reload.

212
00:16:20,730 --> 00:16:25,080
Whatever to the text has this text now so you can provide.

213
00:16:25,230 --> 00:16:28,170
Here is the backslash which creates a new line.

214
00:16:28,980 --> 00:16:34,870
So that is how you can write text and about reading.

215
00:16:34,890 --> 00:16:36,420
The same goes for reading.

216
00:16:36,420 --> 00:16:49,530
So with open whatever the text in read mode and file that's read lines will return a list.

217
00:16:49,950 --> 00:16:56,130
So you see, it's a list file that close closes that file.

218
00:16:57,580 --> 00:17:06,060
Of course, you can also use a file that reads over whatever the text or over whatever to the text.

219
00:17:06,069 --> 00:17:08,890
And what you're going to get is one single string.

220
00:17:08,890 --> 00:17:10,410
So you see the difference.

221
00:17:10,420 --> 00:17:15,099
This gives you a list and the list has multiple string objects.

222
00:17:15,099 --> 00:17:16,210
One, two.

223
00:17:16,990 --> 00:17:20,200
While for the three, it gives you one single string.

224
00:17:20,890 --> 00:17:25,089
You can also see backslash n here, backslash n here.

225
00:17:25,240 --> 00:17:29,470
If you wanted to print out the backslash ends as.

226
00:17:30,330 --> 00:17:31,130
Brake lights.

227
00:17:31,140 --> 00:17:35,730
You want to wrap this inside parentheses and say print.

228
00:17:35,730 --> 00:17:39,000
So the print function gets this as inputs.

229
00:17:41,510 --> 00:17:48,050
Now, this is something interesting what happened here, and we didn't get any output.

230
00:17:49,210 --> 00:17:55,420
So perhaps you were expecting to see line one, break line, line two.

231
00:17:55,450 --> 00:17:56,540
That didn't happen.

232
00:17:56,560 --> 00:18:01,010
Even if you say five that read again, you get an empty string.

233
00:18:01,030 --> 00:18:05,560
That is because the rate operation is exhausted.

234
00:18:05,860 --> 00:18:07,780
So let me explain that to you.

235
00:18:08,590 --> 00:18:09,580
What happened?

236
00:18:10,930 --> 00:18:17,620
So we opened the file here in reads mode and then we read the content.

237
00:18:17,650 --> 00:18:21,400
Now there is something, a concept called the cursor.

238
00:18:22,840 --> 00:18:29,250
So when you just open the file, the cursor is at the very first position.

239
00:18:29,260 --> 00:18:37,960
But then when you apply the file that reads method, the cursor goes and reads the text and then it

240
00:18:37,960 --> 00:18:38,870
stays here.

241
00:18:38,890 --> 00:18:43,270
So after this method is executed, the cursor is here.

242
00:18:43,330 --> 00:18:50,890
Therefore, if you execute file, read again as we did in here, the cursor will try to read again what

243
00:18:50,890 --> 00:18:55,650
whatever is on the right side and down and there's nothing here.

244
00:18:55,660 --> 00:19:01,810
Therefore we get empty outputs here, empty outputs in here.

245
00:19:02,800 --> 00:19:04,810
So that's something to keep in mind.

246
00:19:05,080 --> 00:19:10,060
So the workaround is, of course, to close the file and open the.

247
00:19:12,950 --> 00:19:17,810
File again in red mode for the treat should give you those lines.

248
00:19:19,870 --> 00:19:22,750
So that's about this video.

249
00:19:27,580 --> 00:19:34,390
Being able to store data in text files and to read data from text files via Python is going to be very

250
00:19:34,390 --> 00:19:37,780
beneficial for many, many applications.

251
00:19:38,460 --> 00:19:41,610
I hope this was clear to you, and I'll talk to you later.

