I don't know if you have wondering about it, but we have a big issue with our program.
The issue is that the user enters these to do items, but then if you close the program, those data
are lost.
So if I run the program again and I say show it shows nothing.
And that is, of course, the expected behavior of the program, because we are trying to save the user
data, the to do items in variables.
The problem is that.
When the program ends, its execution, variable values are discarded.
Because they are simply saved in temporary memory on your computer while the program is up and running.
So we need to figure out a better solution.
The solution is to get the user input and store it via Python.
In an external file on disk.
And that is what you're going to learn in this video.
So you're going to learn how to store data in text files.
Now, there are different solutions to store user data.
So one is text files, which is what we're going to use in this lecture.
Text files is suggested to be used because it's a simple solution and it works very well with simple
data.
So in our case, we simply have these sentences.
Now, if you had more complex data, you might want to use CSV format, so CSV files or JSON, and if
you have even more complex data where there is more user interaction with the data, then you might
want to go with the ultimate solution, which is SQL databases.
So we're going to cover all these technologies in the course.
Now let's focus on text files.
So first of all, I would start by creating an empty text file.
So to do that, you can right click over your project directory here.
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
file.
Make sure that the text file is created in the same directory with your main AP file.
So these are in the same folder.
Then go to Maine that p y, and let's implement the ADD case first.
So currently we have an empty list in here and then the user enters a to do item and we append that
to the item to the list.
So to do is update it at this point with a user.
To the item.
Now, we will keep these two lines as they are, but then we want to store the list items in a text
file.
To do that.
You first want to create a file variable and this file variable, you want to store a file object.
The file object is returned by the open function.
So Open produces this certain type of object, a file object, just like we have string objects.
That is a string object.
That is a list object.
We also have file objects.
So these are designed to interact with files on disk with that file.
So to interact with that file.
This method requires the path to that file to dos the text.
And then there is a second argument you want to specify, which is.
A string again, It could be.
W it could be.
Or in this case it's w w means right and R means right.
So if you want to write something in that file.
Via Python.
Then you write.
W if you want to read the content of that file.
So if you want to print out the content in via Python in the command line here, you want to read that
file, so you extract the text from that file, store it in a python string and print it out.
In this case, we want to write.
So that will create a foil object.
And then in that file object, we write lines.
So write lines is a method available for file objects.
And this right line's method gets as arguments a list.
So our list is to deduce therefore the items of this list will be written into those the text.
So let's try this.
I'm going to run the script.
Press add to indicate that you want to add an item clean.
At again through and then exit to exit the program, press enter, and then we want to check to do that
text.
If you don't see anything, try to read, click here and go to real load from disk and that will refresh
the content over to those that text file in your patch already.
So these are the data we entered, not quite as you were expecting, probably because you see that these
are merged together.
There is an easy solution for that, though.
You can simply.
Add a backslash and character after this.
So that part contains the user input.
So clean, for example.
So if you say clean plus that what you get is I can show you that in the console.
So basically we're doing this clean plus backslash n.
So that merges the two strings.
But if you use print.
And you do it again.
Clean.
Plus.
RN and you press enter, then what you get is clean plus a brake line.
So n is a backslash, n is actually a brake line.
We didn't see the brake line here because if you just do clean plus backslash and python will print
out a very raw representation of the string.
So it it does not recognize backslash n as an actual brake line.
But print is designed to convert that symbol.
That is a special symbol, special string.
It converts them that to a break line.
So let's go back to our script.
And here we are.
This again.
Add.
Let's try to prepare this time and add again.
Shower exit.
Hmm.
Let's check to do's.
So prepare and shower.
So now it is working.
Well, these are.
Each in one line.
But there is a problem if you've noticed it, where it's clean and through.
So they have disappeared.
Why is that?
I'll I'll explain that to you.
What happens is that this line here, what it does is if it finds that file in the directory here,
that's methods will override that file.
So basically, it creates a new file with that name.
Then it writes.
The lists in that file.
So if there were other items, other texts in that file, it will be overwritten by these new items.
In this case, prepare and shower.
So this is not the expected behavior we want for the program.
We want to keep the existing to do items and be able to add other items under those two.
So, for example, now we have these two.
If the user enters code in the command line, code should be written in here.
So let's go back and fix this.
The way to fix this is to first get an item from the user and then create a list, a python list.
And this list is going to contain the existing items from the text file.
So first of all, we need to delete that list.
We don't need that list anymore.
So what we want to do is we get the item from the user and then we say file is equal to open.
To dos the text.
Ah, this time.
So we open the file in red mode.
We want to read what is inside that to do that txt file.
And we create a variable to dos, which is equal to file that reads lines.
So that is the equivalent of this.
But to read.
So it will read to the content that is in the file and it will create a list with that content.
Each line will be an item of that list.
So at this point, we have a list with two items and let me make a break line and then we append.
The user's item in that list.
So if the user enters.
Clean than the lists at this point will be something like prepare.
What else?
Shower and clean.
The item that the user just entered.
So something like that.
And then.
Let me break this as well.
And in this part, we.
Creates a new file.
So we completely overwrite the existing file.
And in that new file we write that list which contains three items now.
So let's try this.
At.
Play at.
Fix.
And exit press answer go to to do the text and that's the output.
So we had to prepare and shower earlier.
Now we have play and fix.
So that's code.
Make sure that.
From the user to the items are stored in a text file.
Before we close this video, though, there's something we need to add.
So once you open a file object here and you have finished working with that file object, then you need
to close it.
Things will work even without closing it.
But sometimes.
Different parts of the codes can interact with each other in a way that.
They will change the contents of your file if you have not closed it.
So it's good that once you are done with the file, just close it.
And the same goes for this other one here.
Close.
So you open it here, you close it here, you open it here.
Finish what you do.
You close it here.
Now, this is just a variable here.
You could also have another variable.
Whatever here.
So that would work.
File whatever, file whatever that close.
So it doesn't matter that we happen to have the same variable names in here.
And that's about it.
As a summary, let me open the Python console and show you what we just did.
So we used the open function, and this open function creates a file, whatever.
For example, the txt that is a path to all of the file.
With W you create a new file.
If the file is existing, it will be overwritten with an empty file and then you can write lines to
that file by providing a list of lines.
Line one.
Line two.
You execute that.
Don't forget to close the file using the closed methods and a new directory here.
If you do a reload from disk, for example, you should see whatever the text.
So those are the lines.
So again, you can override that file by opening it again.
Right lines.
And if you want to have brake lines between the lines at backslash.
Inside each string.
So that's one string.
That's the other string.
Backslash n press enter.
File that close.
Reloads the file on par charm and you should see this new output.
It is also possible to write texts in a file, so hear the input.
The argument is a list.
How can you write a simple string?
Well, that is also possible.
Again, we use the open function and let's say whatever does whatever to the text in write mode.
So the code is the same.
But then instead of write lines, you use write, and instead of passing a list, you pass simply text.
Hey, this is just text here and there.
You can provide backslash and characters here as well and press enter and file dots close.
It closes the file.
This is the number of bytes written in that file.
Just for your information, file close and to reload.
Whatever to the text has this text now so you can provide.
Here is the backslash which creates a new line.
So that is how you can write text and about reading.
The same goes for reading.
So with open whatever the text in read mode and file that's read lines will return a list.
So you see, it's a list file that close closes that file.
Of course, you can also use a file that reads over whatever the text or over whatever to the text.
And what you're going to get is one single string.
So you see the difference.
This gives you a list and the list has multiple string objects.
One, two.
While for the three, it gives you one single string.
You can also see backslash n here, backslash n here.
If you wanted to print out the backslash ends as.
Brake lights.
You want to wrap this inside parentheses and say print.
So the print function gets this as inputs.
Now, this is something interesting what happened here, and we didn't get any output.
So perhaps you were expecting to see line one, break line, line two.
That didn't happen.
Even if you say five that read again, you get an empty string.
That is because the rate operation is exhausted.
So let me explain that to you.
What happened?
So we opened the file here in reads mode and then we read the content.
Now there is something, a concept called the cursor.
So when you just open the file, the cursor is at the very first position.
But then when you apply the file that reads method, the cursor goes and reads the text and then it
stays here.
So after this method is executed, the cursor is here.
Therefore, if you execute file, read again as we did in here, the cursor will try to read again what
whatever is on the right side and down and there's nothing here.
Therefore we get empty outputs here, empty outputs in here.
So that's something to keep in mind.
So the workaround is, of course, to close the file and open the.
File again in red mode for the treat should give you those lines.
So that's about this video.
Being able to store data in text files and to read data from text files via Python is going to be very
beneficial for many, many applications.
I hope this was clear to you, and I'll talk to you later.