WEBVTT

00:00.080 --> 00:03.530
And let's start step number three for this final project.

00:03.530 --> 00:05.060
So what do we want to do here?

00:05.060 --> 00:10.820
We want to write each file name for each photo that we take inside a log file.

00:10.850 --> 00:14.570
So I'm going to go back to my photos Finder project folder.

00:14.570 --> 00:19.160
And I'm also going to remove those files for now okay.

00:19.790 --> 00:25.340
So when we take a photo we also want to add the file name to a log file okay.

00:25.370 --> 00:28.520
That's going to be useful for the step five and six.

00:28.520 --> 00:33.830
When we want to get access to the last photos and display them on a web page.

00:33.860 --> 00:34.250
All right.

00:34.250 --> 00:36.590
So here we don't have any text file.

00:36.590 --> 00:39.260
We're going to create a text file from the program.

00:39.260 --> 00:43.790
So when we start the program we're going to actually remove the text file.

00:43.790 --> 00:47.120
So we're going to start from a fresh log file every time.

00:47.120 --> 00:51.320
And when we add a photo in the program we add the file name.

00:51.320 --> 00:55.880
So we add every time a file name to a new line inside the log file.

00:55.910 --> 00:56.180
All right.

00:56.210 --> 00:57.920
So let's get started with this.

00:57.920 --> 01:03.260
So I'm going to create a log file name here in my global variables.

01:03.350 --> 01:08.690
Here I'm going to name it log file name.

01:08.690 --> 01:13.370
And that's going to be what that's going to be inside the camera folder.

01:13.370 --> 01:16.580
So I'm just gonna use that one.

01:17.420 --> 01:26.630
And then plus let's name it photos maybe photo logs dot txt.

01:26.660 --> 01:32.720
I'm going to add a txt extension just in case we want to export this to another operating system.

01:32.750 --> 01:37.250
All right so it's going to be photo logs dot txt inside this folder.

01:37.250 --> 01:42.530
And now for example after the camera before the Gpio it doesn't really matter.

01:42.530 --> 01:50.840
Here I'm gonna do a remove log file as a setup step.

01:50.840 --> 01:53.030
So the log file doesn't exist yet.

01:53.030 --> 01:56.030
But when I'm going to create one it's going to be here.

01:56.030 --> 01:59.220
So we want to remove in case there is one I'm going to do.

01:59.250 --> 02:05.310
If OS dot path dot exists with the log file name.

02:05.340 --> 02:09.060
Okay, so I make sure that it exists before I remove it.

02:09.180 --> 02:12.060
If it doesn't exist of course I don't remove it.

02:12.630 --> 02:17.850
So I do OS remove with the log file name.

02:18.270 --> 02:19.470
And I'm also going to print.

02:19.470 --> 02:21.270
So only if we remove it.

02:21.540 --> 02:26.610
Let's say previous log file removed.

02:27.630 --> 02:27.990
All right.

02:27.990 --> 02:28.920
So that's it.

02:28.950 --> 02:31.980
We just remove the file if it exists.

02:31.980 --> 02:37.140
And then to add a line inside this log file I'm going to add another function here.

02:37.170 --> 02:40.590
I'm going to add it right after take photo.

02:40.980 --> 02:46.470
So let's name this one update photo.

02:47.370 --> 02:48.990
Log file for example.

02:48.990 --> 02:51.750
And what do we get as parameters.

02:51.780 --> 02:57.140
Well we're going to get the log file name to know where we need to write the log.

02:57.140 --> 03:01.760
And also we're going to get the photo file name.

03:02.030 --> 03:02.450
Okay.

03:02.480 --> 03:09.380
So once again I could use the log file name and put it before that function and use the global variable

03:09.380 --> 03:10.070
inside.

03:10.070 --> 03:12.680
But I'm just going to use instead a parameter.

03:12.710 --> 03:12.950
Okay.

03:12.980 --> 03:18.050
So basically we could take those functions here and put them somewhere else.

03:18.050 --> 03:19.790
And they could also work.

03:19.820 --> 03:20.000
Okay.

03:20.030 --> 03:23.600
We just need to provide the log file name as an argument.

03:23.600 --> 03:25.850
So let's write this function.

03:25.850 --> 03:26.780
What do we want to do.

03:26.780 --> 03:28.550
We want to open the file.

03:28.580 --> 03:34.370
To open the file with we've open log file name.

03:35.450 --> 03:37.730
And then what mode do we want.

03:37.760 --> 03:39.080
Do we want to read.

03:39.110 --> 03:40.460
No we want to write.

03:40.460 --> 03:43.130
But do we want to erase everything and write.

03:43.160 --> 03:45.440
No we want to append to the file.

03:45.440 --> 03:48.020
So I'm going to use the A mode okay.

03:48.050 --> 03:50.930
We're going to open the file and open a new line.

03:50.960 --> 03:53.480
I'm going to do as f okay.

03:53.480 --> 03:55.660
So we can do f dot Right.

03:55.660 --> 03:56.710
And what do we write?

03:56.710 --> 04:04.750
Well, we write the photo file name and then we're going to need also a new line character.

04:04.750 --> 04:07.570
But I'm going to start like this and show you what's going to happen okay.

04:07.600 --> 04:13.270
So every time we call this function we add something to the file.

04:13.270 --> 04:15.280
And that's going to be the photo file name.

04:15.280 --> 04:17.920
So now that I have my function I can call it.

04:17.920 --> 04:27.010
So update photo log file I can call it here update photo log file.

04:27.010 --> 04:28.300
So you see that here.

04:28.300 --> 04:29.200
What we do.

04:29.230 --> 04:33.070
This is kind of the main logic here for the program.

04:33.070 --> 04:37.150
And what we do is we just call the different functions that we have created.

04:37.150 --> 04:47.620
So here I call update photo log file with log file name and also the photo file name that I just got

04:47.650 --> 04:49.750
here when I took the photo.

04:50.950 --> 04:53.410
Photo file name.

04:53.740 --> 04:58.370
All right, so when I take a photo and I save the photo, I also save a log.

04:58.430 --> 05:00.170
Okay, let's save that.

05:00.170 --> 05:03.080
And actually I saved it as project step two.

05:03.110 --> 05:06.710
But I'm just gonna save it as step three.

05:06.740 --> 05:09.110
Then I'm going to modify step two again.

05:09.110 --> 05:13.040
So you can download the correct Python file for each step.

05:13.040 --> 05:15.020
So project step three.

05:15.050 --> 05:16.430
Let's go back here.

05:16.670 --> 05:16.940
Okay.

05:16.940 --> 05:18.980
Let's stop whatever we had running.

05:19.010 --> 05:21.980
I'm also going to remove everything from this folder.

05:24.020 --> 05:24.740
Okay.

05:25.850 --> 05:32.210
And let's do Python three project step three.

05:34.670 --> 05:36.050
So camera.

05:36.080 --> 05:37.190
Okay Gpio.

05:37.220 --> 05:37.520
Okay.

05:37.550 --> 05:43.760
You see that we don't have this log because there was no log file.

05:44.810 --> 05:45.200
Okay.

05:45.200 --> 05:49.010
So now I'm going to take a few pictures or at least two.

05:50.720 --> 05:51.020
All right.

05:51.020 --> 05:52.160
This is the first one.

05:52.720 --> 05:54.190
Let's see what we have.

05:54.790 --> 06:03.460
We have the photo and we have the photo log.txt with you see the path to the image.

06:04.660 --> 06:05.320
All right.

06:06.100 --> 06:06.490
All right.

06:06.490 --> 06:08.350
So we took a second photo.

06:08.350 --> 06:13.120
And you can see here we have the second photo and the log is updated.

06:13.120 --> 06:17.710
But as you can see we just add the path right after the first one.

06:17.710 --> 06:24.730
So if I want to have one in each line I also need to add it's going to be in this function.

06:24.730 --> 06:27.880
Here I could do f dot write for the file name.

06:27.880 --> 06:33.160
And then I can do f dot write with a backslash n character.

06:33.190 --> 06:35.650
Okay let's save that.

06:36.490 --> 06:42.370
And let's let's just close it and let's stop the program.

06:43.660 --> 06:43.900
Okay.

06:43.930 --> 06:46.840
And you can see here we still have the log file.

06:46.870 --> 06:48.970
But I'm going to start the program again.

06:50.680 --> 06:57.180
And it's going to previous you see previous log file removed because it existed.

06:57.180 --> 06:58.860
Now we don't have any log file.

06:58.890 --> 06:59.460
Okay.

06:59.460 --> 07:02.370
When we take a photo I'm going to take a photo.

07:03.480 --> 07:03.780
All right.

07:03.780 --> 07:07.710
You see we took a photo and we have the log file that's also created.

07:07.710 --> 07:09.510
I'm going to take a second photo.

07:09.510 --> 07:11.760
So wait a bit and take a second one.

07:12.600 --> 07:12.990
All right.

07:12.990 --> 07:14.880
So I took another picture.

07:14.880 --> 07:18.450
And now you can see we have one log per line.

07:18.450 --> 07:20.430
So one file name per line.

07:20.430 --> 07:21.720
And that's basically it.

07:21.720 --> 07:25.470
So to recap we have created a.

07:25.500 --> 07:30.540
So we have a log file name here that we create using the camera folder path.

07:31.350 --> 07:34.920
We remove the log file at the beginning if it already exists okay.

07:34.950 --> 07:42.570
And then when we take a photo right after taking the photo we update the photo log file with this function

07:43.260 --> 07:49.950
here, where we open it, we append the new photo file name after the other ones, and that's pretty

07:49.950 --> 07:50.640
much it.
