And let's start step number three for this final project.
So what do we want to do here?
We want to write each file name for each photo that we take inside a log file.
So I'm going to go back to my photos Finder project folder.
And I'm also going to remove those files for now okay.
So when we take a photo we also want to add the file name to a log file okay.
That's going to be useful for the step five and six.
When we want to get access to the last photos and display them on a web page.
All right.
So here we don't have any text file.
We're going to create a text file from the program.
So when we start the program we're going to actually remove the text file.
So we're going to start from a fresh log file every time.
And when we add a photo in the program we add the file name.
So we add every time a file name to a new line inside the log file.
All right.
So let's get started with this.
So I'm going to create a log file name here in my global variables.
Here I'm going to name it log file name.
And that's going to be what that's going to be inside the camera folder.
So I'm just gonna use that one.
And then plus let's name it photos maybe photo logs dot txt.
I'm going to add a txt extension just in case we want to export this to another operating system.
All right so it's going to be photo logs dot txt inside this folder.
And now for example after the camera before the Gpio it doesn't really matter.
Here I'm gonna do a remove log file as a setup step.
So the log file doesn't exist yet.
But when I'm going to create one it's going to be here.
So we want to remove in case there is one I'm going to do.
If OS dot path dot exists with the log file name.
Okay, so I make sure that it exists before I remove it.
If it doesn't exist of course I don't remove it.
So I do OS remove with the log file name.
And I'm also going to print.
So only if we remove it.
Let's say previous log file removed.
All right.
So that's it.
We just remove the file if it exists.
And then to add a line inside this log file I'm going to add another function here.
I'm going to add it right after take photo.
So let's name this one update photo.
Log file for example.
And what do we get as parameters.
Well we're going to get the log file name to know where we need to write the log.
And also we're going to get the photo file name.
Okay.
So once again I could use the log file name and put it before that function and use the global variable
inside.
But I'm just going to use instead a parameter.
Okay.
So basically we could take those functions here and put them somewhere else.
And they could also work.
Okay.
We just need to provide the log file name as an argument.
So let's write this function.
What do we want to do.
We want to open the file.
To open the file with we've open log file name.
And then what mode do we want.
Do we want to read.
No we want to write.
But do we want to erase everything and write.
No we want to append to the file.
So I'm going to use the A mode okay.
We're going to open the file and open a new line.
I'm going to do as f okay.
So we can do f dot Right.
And what do we write?
Well, we write the photo file name and then we're going to need also a new line character.
But I'm going to start like this and show you what's going to happen okay.
So every time we call this function we add something to the file.
And that's going to be the photo file name.
So now that I have my function I can call it.
So update photo log file I can call it here update photo log file.
So you see that here.
What we do.
This is kind of the main logic here for the program.
And what we do is we just call the different functions that we have created.
So here I call update photo log file with log file name and also the photo file name that I just got
here when I took the photo.
Photo file name.
All right, so when I take a photo and I save the photo, I also save a log.
Okay, let's save that.
And actually I saved it as project step two.
But I'm just gonna save it as step three.
Then I'm going to modify step two again.
So you can download the correct Python file for each step.
So project step three.
Let's go back here.
Okay.
Let's stop whatever we had running.
I'm also going to remove everything from this folder.
Okay.
And let's do Python three project step three.
So camera.
Okay Gpio.
Okay.
You see that we don't have this log because there was no log file.
Okay.
So now I'm going to take a few pictures or at least two.
All right.
This is the first one.
Let's see what we have.
We have the photo and we have the photo log.txt with you see the path to the image.
All right.
So we took a second photo.
And you can see here we have the second photo and the log is updated.
But as you can see we just add the path right after the first one.
So if I want to have one in each line I also need to add it's going to be in this function.
Here I could do f dot write for the file name.
And then I can do f dot write with a backslash n character.
Okay let's save that.
And let's let's just close it and let's stop the program.
Okay.
And you can see here we still have the log file.
But I'm going to start the program again.
And it's going to previous you see previous log file removed because it existed.
Now we don't have any log file.
Okay.
When we take a photo I'm going to take a photo.
All right.
You see we took a photo and we have the log file that's also created.
I'm going to take a second photo.
So wait a bit and take a second one.
All right.
So I took another picture.
And now you can see we have one log per line.
So one file name per line.
And that's basically it.
So to recap we have created a.
So we have a log file name here that we create using the camera folder path.
We remove the log file at the beginning if it already exists okay.
And then when we take a photo right after taking the photo we update the photo log file with this function
here, where we open it, we append the new photo file name after the other ones, and that's pretty
much it.