WEBVTT

00:00.080 --> 00:02.840
There is one more thing I want to show you here.

00:02.840 --> 00:06.800
And we're going to use that actually in the final project for this course.

00:06.830 --> 00:11.060
And this is how to read, write and manipulate files with Python.

00:11.060 --> 00:14.210
And first I'm going to create a new file with a few lines okay.

00:14.240 --> 00:17.210
And then we're going to try to read this file with Python.

00:17.210 --> 00:21.320
Then maybe to add some text to replace the text to even remove the file okay.

00:21.350 --> 00:22.790
So let's create a file.

00:22.820 --> 00:26.960
I'm well I use CD to be in my home directory.

00:26.960 --> 00:30.290
And then let's just create a simple file so I could do touch.

00:30.290 --> 00:37.370
But because I'm going to write a few lines, I'm just going to use Nano and let's name it text file.

00:37.400 --> 00:37.760
Okay.

00:37.790 --> 00:39.620
With no extension it doesn't really matter.

00:39.620 --> 00:44.300
So I create a file and let's say this is line one.

00:45.140 --> 00:48.650
This is line two and line three okay.

00:48.680 --> 00:51.020
Just write some random text, just three lines.

00:51.020 --> 00:55.070
I'm going to do Ctrl S to save and Ctrl X to exit.

00:55.070 --> 00:59.000
And we can see we've got text file okay.

00:59.030 --> 00:59.900
We have the three lines.

00:59.900 --> 01:05.380
So you can see in this case using nano was very efficient because you see, with just a few comments

01:05.380 --> 01:10.240
and a few seconds, we create a file, we write to a file, we save it and we don't need to open any

01:10.240 --> 01:10.900
other program.

01:10.900 --> 01:13.240
Now I'm going to create a Python file.

01:13.270 --> 01:17.230
So I could do that from the terminal or from Tony.

01:17.650 --> 01:17.980
Okay.

01:18.010 --> 01:20.050
It's going to be the same for this.

01:20.950 --> 01:25.120
Let's just create a new file okay.

01:25.150 --> 01:32.920
I'm just going to save it as for example read write.py in my Python programs.

01:34.090 --> 01:40.030
Now the first thing we want to do is well, how to actually read from that file that we have created

01:40.060 --> 01:40.360
here.

01:40.390 --> 01:42.520
So we want to read this text.

01:42.640 --> 01:45.280
So I'm just going to write the instruction and then explain it to you.

01:45.310 --> 01:51.250
We're going to start with a keyword with and then open function to open the file.

01:51.250 --> 01:54.100
I'm going to put the file so the path to the file.

01:54.100 --> 01:57.130
And here let's try to use the absolute path.

01:57.130 --> 02:02.990
So you see if I do PWD this is home slash API slash text file.

02:03.020 --> 02:03.560
Okay.

02:04.340 --> 02:10.010
So slash home slash API slash text file.

02:10.040 --> 02:15.440
This is a good use of the absolute path, because you don't necessarily know where you will want to

02:15.470 --> 02:16.940
run the Python script.

02:16.940 --> 02:22.580
So depending on where the Python script is, the relative path to that text file might be different.

02:22.610 --> 02:25.670
Here we are sure that we provide the absolute path.

02:25.670 --> 02:28.370
So we are sure that this is going to be the correct one.

02:28.370 --> 02:35.150
So I put the path and then I will put here with another double quotes are for reading.

02:35.180 --> 02:38.480
And I'm going to come back to the different options you can put here in a minute.

02:38.510 --> 02:42.920
I close the parentheses and I do as f and then a colon.

02:42.920 --> 02:49.550
I go back to a new line and I have an indentation, and I can use here this variable f to read from

02:49.550 --> 02:50.060
the file.

02:50.060 --> 02:52.370
So what's happening here with this first line.

02:52.370 --> 02:56.870
As I told you we have this open function that's going to open a file.

02:56.990 --> 03:00.100
So you have the path to the file and then what you want to do.

03:00.100 --> 03:02.170
So for now we want to read from the file.

03:02.170 --> 03:03.820
So we put R okay.

03:03.820 --> 03:07.930
Which means you will only be able to read from the file and not to write to it.

03:07.930 --> 03:10.540
Then we have as f here.

03:10.540 --> 03:12.790
So f as I told you it's going to be.

03:12.790 --> 03:19.360
Well the content of the file is going to be inside F, so you will be able to interact with the content

03:19.390 --> 03:21.430
using F and why f.

03:21.460 --> 03:22.990
Well it's kind of a convention.

03:22.990 --> 03:26.830
For example for a for loop or a while loop you use I as an index.

03:26.830 --> 03:29.500
Here we use f as a file okay.

03:29.530 --> 03:32.440
It's just a variable that's going to be available for us to use.

03:32.470 --> 03:34.030
And then we have the with keyword.

03:34.030 --> 03:34.840
Why do we have that.

03:34.840 --> 03:39.280
Because well actually when you need to open a file you also need to close it later.

03:39.280 --> 03:43.390
And this can be a bit tricky, especially if you have an error in the middle.

03:43.420 --> 03:46.300
The file might not be closed correctly.

03:46.300 --> 03:48.280
So by using the with keywords.

03:48.280 --> 03:55.510
So with open as variable, you are sure that the file will be correctly opened and closed.

03:55.540 --> 03:55.810
All right.

03:55.840 --> 03:57.790
So no need to worry about this anymore.

03:57.820 --> 04:05.370
Now if I want to read the content of the file if I want to get everything I can do f dot read just like

04:05.370 --> 04:10.620
that, and I can put this inside a print to print.

04:10.620 --> 04:12.600
And then I add a parenthesis here.

04:12.600 --> 04:15.270
Let's just run that from here for example.

04:16.170 --> 04:18.900
And you see we have this is line one, line two and line three.

04:18.900 --> 04:20.430
So we have all the lines.

04:20.430 --> 04:23.370
So we can just get the whole content from the file.

04:23.370 --> 04:25.860
Now I'm going to put a comment here.

04:26.160 --> 04:27.510
I'm going to comment this line.

04:27.540 --> 04:34.440
What if instead of just getting the whole text, you want to get the text line by line, you can do

04:34.440 --> 04:34.710
this.

04:34.710 --> 04:36.240
You can use a for loop.

04:36.240 --> 04:45.270
You can do for line in F, just as if the file was actually a list containing all the lines, and each

04:45.270 --> 04:46.620
line is one element.

04:46.620 --> 04:51.810
So for line in F and then you just do print line okay.

04:51.840 --> 04:53.190
Let's see what we have.

04:53.790 --> 04:55.650
You see we also have the three lines.

04:55.650 --> 04:59.410
And note that we have an extra space here between each line.

04:59.440 --> 04:59.830
All right.

04:59.830 --> 05:05.770
So with this you can see you have the complete text of each line one by one.

05:05.770 --> 05:09.010
Now what if you want to write to a file.

05:09.010 --> 05:13.870
So if you want to write to a file instead of R you're going to put W.

05:13.900 --> 05:14.920
Then we're not going to read.

05:14.920 --> 05:17.620
We're going to write something else here.

05:17.620 --> 05:24.190
For example f dot write and let's say new new text.

05:24.220 --> 05:24.640
All right.

05:24.640 --> 05:26.920
So we open the file with the writing mode.

05:27.070 --> 05:28.900
And then we just write new text.

05:28.930 --> 05:30.760
Let's see what's going to happen.

05:30.910 --> 05:34.120
So I run the script but we don't have any output here.

05:34.120 --> 05:36.850
But now let's see the file.

05:36.850 --> 05:38.260
So the file is still here.

05:38.290 --> 05:43.330
We have the text file and what's inside the text file.

05:43.330 --> 05:47.530
Well you see that everything is gone and we just have new text.

05:47.530 --> 05:55.000
So if you open the file with W and you write something this is going to replace the whole text with

05:55.000 --> 05:57.190
the new text from the program.

05:57.220 --> 05:57.580
Okay.

05:57.610 --> 06:02.080
One thing you can notice is the text directly ends here.

06:02.110 --> 06:03.670
Can we have the next.

06:03.700 --> 06:07.510
Command prompt right here if you want to add a new line character.

06:07.510 --> 06:08.140
So for example to.

06:08.170 --> 06:10.720
Say that you go back to a new line here.

06:10.720 --> 06:14.650
You can do that inside the text with backslash n.

06:14.680 --> 06:18.940
So this is one of the most common character here backslash n.

06:18.970 --> 06:20.170
You go back to a new line.

06:20.170 --> 06:22.540
So let's see what it does.

06:22.540 --> 06:28.840
If I run again now you will see that I go back to a new line.

06:28.870 --> 06:29.350
Okay.

06:29.380 --> 06:33.670
Because going back to a new line is actually something you need to specify.

06:33.670 --> 06:34.240
And one more.

06:34.270 --> 06:40.360
Thing is that if you open the file like this, let's say that I remove the file with REM.

06:40.660 --> 06:43.870
So REM text file.

06:43.870 --> 06:46.150
So it doesn't exist anymore.

06:46.390 --> 06:47.800
You see it's gone.

06:47.800 --> 06:50.590
If I run the program again it's still working.

06:50.590 --> 06:53.830
And you see that we have the text file here.

06:53.830 --> 06:56.080
So this w command is going to write.

06:56.080 --> 07:01.450
But not only right it's going to check first if the file exists and if it doesn't exist, it's going

07:01.450 --> 07:03.220
to create it automatically.

07:03.250 --> 07:03.610
Great.

07:03.610 --> 07:08.890
So now you can read how you can write what if you want to read and to write from a file at the same

07:08.890 --> 07:11.140
time you can do W plus.

07:11.140 --> 07:13.690
So with W plus you can read the file.

07:13.690 --> 07:17.290
And you can also write to it by erasing its contents.

07:17.290 --> 07:22.660
And finally, what if you actually want to just append text so you don't want to rewrite the entire

07:22.660 --> 07:22.870
file.

07:22.870 --> 07:25.390
You just want to add text at the end of the file.

07:25.390 --> 07:27.580
You can use the A mode.

07:27.610 --> 07:27.820
Okay.

07:27.850 --> 07:34.720
So for now we have you can see we have this text file that contains that one line with new text.

07:34.750 --> 07:38.860
What if I run this code here with the A mode.

07:39.460 --> 07:40.450
Let's run that.

07:41.200 --> 07:44.080
And let's go back here and let's see what do we have inside.

07:44.110 --> 07:48.190
Now we have new text and new text.

07:48.190 --> 07:52.540
So you see that line was added at the end of the program.

07:52.540 --> 08:00.450
If I run it twice more then I go back here And you see now I have four lines.

08:00.480 --> 08:00.780
All right.

08:00.780 --> 08:04.680
So if you just want to add text to the end of the file you use a.

08:04.710 --> 08:06.570
So R is to read.

08:06.600 --> 08:10.950
W is to write w plus is to read and write.

08:10.950 --> 08:13.470
And a is to append to the end.

08:13.500 --> 08:13.920
Great.

08:13.920 --> 08:19.050
And to finish with this I'm going to show you an additional Python module that allows you to do stuff

08:19.050 --> 08:25.350
like for example here, if you want to do the RM command from the terminal, well you can also use this

08:25.350 --> 08:27.510
directly from a Python script.

08:27.540 --> 08:33.420
And this is what we're going to need to import the OS module.

08:33.450 --> 08:35.370
So this is a built in module.

08:35.370 --> 08:36.990
You don't need to install it.

08:36.990 --> 08:38.910
It's already there for you to use.

08:38.910 --> 08:41.700
So you just need to do import OS in your code.

08:42.090 --> 08:44.700
I'm going to comment those.

08:44.700 --> 08:46.620
So toggle comments.

08:46.620 --> 08:54.090
And well one thing that will be quite useful for us in this course is to check first if a file exists,

08:54.090 --> 09:00.260
because for example, if you want to open a file for reading, you might want to check before if it

09:00.290 --> 09:01.940
exists and then you open it.

09:01.970 --> 09:04.190
If it doesn't exist, maybe you don't open it.

09:04.190 --> 09:10.460
And so we're going to do if and then we have OS dot path dot exists.

09:10.490 --> 09:11.780
That's the function.

09:11.840 --> 09:13.820
And we need to provide a path.

09:13.820 --> 09:19.940
So here I'm just going to copy this path to this file okay.

09:19.940 --> 09:23.060
And this function is going to return true or false.

09:23.060 --> 09:28.100
So I put it inside the if and if this file exists I'm going to print.

09:28.340 --> 09:29.450
Um okay.

09:29.630 --> 09:31.700
So let's just run that here.

09:32.360 --> 09:33.380
You see okay.

09:33.380 --> 09:37.430
It exists if I put a file name that doesn't exist.

09:37.430 --> 09:39.650
So for example I add some letters here.

09:39.650 --> 09:41.180
You see we won't have okay.

09:41.210 --> 09:49.220
So if you provide the wrong path or if you removed the file before you see that you won't have this.

09:49.220 --> 09:50.330
So this will not return.

09:50.330 --> 09:50.780
True.

09:50.780 --> 09:53.930
And now what if you want to remove a file.

09:53.930 --> 09:57.810
Well, to remove a file, it could be nice to see if it exists first.

09:57.840 --> 10:00.270
That's kind of what we do here in the terminal.

10:00.270 --> 10:03.720
You're going to do for example, LZ, you're going to check, oh, this file exists.

10:03.720 --> 10:06.330
Then I remove it with the RM command.

10:06.330 --> 10:07.950
So same thing with Python.

10:07.950 --> 10:10.260
You're going to check does the file exist.

10:10.260 --> 10:14.880
If yes let's remove the file I'm going to still print okay.

10:14.880 --> 10:15.780
And remove the file.

10:15.780 --> 10:16.950
How to remove the file.

10:16.950 --> 10:23.580
Well you just do OS dot remove and then you put the path to the file.

10:23.580 --> 10:24.750
So I'm going to copy this.

10:24.750 --> 10:28.260
You could put the path inside a variable as you want okay.

10:29.370 --> 10:31.410
Here I just keep it like this.

10:31.410 --> 10:34.410
So if it exists I remove it.

10:34.410 --> 10:36.360
So let's run that.

10:36.360 --> 10:39.990
You see we have okay because it was found and then we remove it.

10:39.990 --> 10:42.990
Let's actually see what we have on the terminal.

10:43.470 --> 10:45.030
And you see the file is gone.

10:45.030 --> 10:47.970
If I run the program again we don't have okay.

10:47.970 --> 10:49.890
Because the file doesn't exist.

10:49.890 --> 10:52.020
And thus we also don't remove it.

10:52.050 --> 10:52.470
All right.

10:52.470 --> 10:55.110
So that's it with Python and the terminal.

10:55.110 --> 10:58.110
Now let's practice a bit more with a new activity.
