There is one more thing I want to show you here.
And we're going to use that actually in the final project for this course.
And this is how to read, write and manipulate files with Python.
And first I'm going to create a new file with a few lines okay.
And then we're going to try to read this file with Python.
Then maybe to add some text to replace the text to even remove the file okay.
So let's create a file.
I'm well I use CD to be in my home directory.
And then let's just create a simple file so I could do touch.
But because I'm going to write a few lines, I'm just going to use Nano and let's name it text file.
Okay.
With no extension it doesn't really matter.
So I create a file and let's say this is line one.
This is line two and line three okay.
Just write some random text, just three lines.
I'm going to do Ctrl S to save and Ctrl X to exit.
And we can see we've got text file okay.
We have the three lines.
So you can see in this case using nano was very efficient because you see, with just a few comments
and a few seconds, we create a file, we write to a file, we save it and we don't need to open any
other program.
Now I'm going to create a Python file.
So I could do that from the terminal or from Tony.
Okay.
It's going to be the same for this.
Let's just create a new file okay.
I'm just going to save it as for example read write.py in my Python programs.
Now the first thing we want to do is well, how to actually read from that file that we have created
here.
So we want to read this text.
So I'm just going to write the instruction and then explain it to you.
We're going to start with a keyword with and then open function to open the file.
I'm going to put the file so the path to the file.
And here let's try to use the absolute path.
So you see if I do PWD this is home slash API slash text file.
Okay.
So slash home slash API slash text file.
This is a good use of the absolute path, because you don't necessarily know where you will want to
run the Python script.
So depending on where the Python script is, the relative path to that text file might be different.
Here we are sure that we provide the absolute path.
So we are sure that this is going to be the correct one.
So I put the path and then I will put here with another double quotes are for reading.
And I'm going to come back to the different options you can put here in a minute.
I close the parentheses and I do as f and then a colon.
I go back to a new line and I have an indentation, and I can use here this variable f to read from
the file.
So what's happening here with this first line.
As I told you we have this open function that's going to open a file.
So you have the path to the file and then what you want to do.
So for now we want to read from the file.
So we put R okay.
Which means you will only be able to read from the file and not to write to it.
Then we have as f here.
So f as I told you it's going to be.
Well the content of the file is going to be inside F, so you will be able to interact with the content
using F and why f.
Well it's kind of a convention.
For example for a for loop or a while loop you use I as an index.
Here we use f as a file okay.
It's just a variable that's going to be available for us to use.
And then we have the with keyword.
Why do we have that.
Because well actually when you need to open a file you also need to close it later.
And this can be a bit tricky, especially if you have an error in the middle.
The file might not be closed correctly.
So by using the with keywords.
So with open as variable, you are sure that the file will be correctly opened and closed.
All right.
So no need to worry about this anymore.
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
that, and I can put this inside a print to print.
And then I add a parenthesis here.
Let's just run that from here for example.
And you see we have this is line one, line two and line three.
So we have all the lines.
So we can just get the whole content from the file.
Now I'm going to put a comment here.
I'm going to comment this line.
What if instead of just getting the whole text, you want to get the text line by line, you can do
this.
You can use a for loop.
You can do for line in F, just as if the file was actually a list containing all the lines, and each
line is one element.
So for line in F and then you just do print line okay.
Let's see what we have.
You see we also have the three lines.
And note that we have an extra space here between each line.
All right.
So with this you can see you have the complete text of each line one by one.
Now what if you want to write to a file.
So if you want to write to a file instead of R you're going to put W.
Then we're not going to read.
We're going to write something else here.
For example f dot write and let's say new new text.
All right.
So we open the file with the writing mode.
And then we just write new text.
Let's see what's going to happen.
So I run the script but we don't have any output here.
But now let's see the file.
So the file is still here.
We have the text file and what's inside the text file.
Well you see that everything is gone and we just have new text.
So if you open the file with W and you write something this is going to replace the whole text with
the new text from the program.
Okay.
One thing you can notice is the text directly ends here.
Can we have the next.
Command prompt right here if you want to add a new line character.
So for example to.
Say that you go back to a new line here.
You can do that inside the text with backslash n.
So this is one of the most common character here backslash n.
You go back to a new line.
So let's see what it does.
If I run again now you will see that I go back to a new line.
Okay.
Because going back to a new line is actually something you need to specify.
And one more.
Thing is that if you open the file like this, let's say that I remove the file with REM.
So REM text file.
So it doesn't exist anymore.
You see it's gone.
If I run the program again it's still working.
And you see that we have the text file here.
So this w command is going to write.
But not only right it's going to check first if the file exists and if it doesn't exist, it's going
to create it automatically.
Great.
So now you can read how you can write what if you want to read and to write from a file at the same
time you can do W plus.
So with W plus you can read the file.
And you can also write to it by erasing its contents.
And finally, what if you actually want to just append text so you don't want to rewrite the entire
file.
You just want to add text at the end of the file.
You can use the A mode.
Okay.
So for now we have you can see we have this text file that contains that one line with new text.
What if I run this code here with the A mode.
Let's run that.
And let's go back here and let's see what do we have inside.
Now we have new text and new text.
So you see that line was added at the end of the program.
If I run it twice more then I go back here And you see now I have four lines.
All right.
So if you just want to add text to the end of the file you use a.
So R is to read.
W is to write w plus is to read and write.
And a is to append to the end.
Great.
And to finish with this I'm going to show you an additional Python module that allows you to do stuff
like for example here, if you want to do the RM command from the terminal, well you can also use this
directly from a Python script.
And this is what we're going to need to import the OS module.
So this is a built in module.
You don't need to install it.
It's already there for you to use.
So you just need to do import OS in your code.
I'm going to comment those.
So toggle comments.
And well one thing that will be quite useful for us in this course is to check first if a file exists,
because for example, if you want to open a file for reading, you might want to check before if it
exists and then you open it.
If it doesn't exist, maybe you don't open it.
And so we're going to do if and then we have OS dot path dot exists.
That's the function.
And we need to provide a path.
So here I'm just going to copy this path to this file okay.
And this function is going to return true or false.
So I put it inside the if and if this file exists I'm going to print.
Um okay.
So let's just run that here.
You see okay.
It exists if I put a file name that doesn't exist.
So for example I add some letters here.
You see we won't have okay.
So if you provide the wrong path or if you removed the file before you see that you won't have this.
So this will not return.
True.
And now what if you want to remove a file.
Well, to remove a file, it could be nice to see if it exists first.
That's kind of what we do here in the terminal.
You're going to do for example, LZ, you're going to check, oh, this file exists.
Then I remove it with the RM command.
So same thing with Python.
You're going to check does the file exist.
If yes let's remove the file I'm going to still print okay.
And remove the file.
How to remove the file.
Well you just do OS dot remove and then you put the path to the file.
So I'm going to copy this.
You could put the path inside a variable as you want okay.
Here I just keep it like this.
So if it exists I remove it.
So let's run that.
You see we have okay because it was found and then we remove it.
Let's actually see what we have on the terminal.
And you see the file is gone.
If I run the program again we don't have okay.
Because the file doesn't exist.
And thus we also don't remove it.
All right.
So that's it with Python and the terminal.
Now let's practice a bit more with a new activity.