Welcome to this lecture in this lecture we're going to see the reading and writing files in the python.
So when you were working with the python files you don't need to import a library in order to read or
write the file so it is very easy to read and write the file object in the python.
So all you need to do is call some of the inbuilt function and just semantic plate and just get that
file object and close the file that you just opened.
So all you need to do is simply first of all user with a statement to do that.
So before that I want to make a quick program over here which will just get input from the user and
show you how to write.
Add to that a file uses open and read from that file.
So first of all I'll say the input variable is going to input us that is into what you love.
It will python.
So this will be input.
I want to strip if there is any empty space or blank space.
So after that we need to the sobriety to the file.
So we have to make use of this witness statement so you can work with this file object using this with
his statement and it is basically designed to provide a much cleaner syntax and exception handling technique
when you are working with the code.
And this is the best practice to work with this with his statement because the bonus of using this method
is that any file open will be close automatically after you are done.
So this is really important feature of a width.
So we'll start using this with if you work in the file handling or a file input output.
So as a with an open we have to first open that file in order to read or write so you can see the intelligence
is giving us a suggestion about the file and the more do you want to open an order are optional over
here.
So first of all I want to give the name to the file so I'll say fun funny about Python and the city
still which is fond of the city and I want to open it in the mall.
So this mall should be either read or write so I want open to the right mode so I'll see it right in
Israel.
So we are aligning this file is year from now.
And I want to write to this file with the input that we just got from the user.
So this is simple is here.
So if you are opening any file in the right mode any of that file doesn't exist.
It is going to create a new file and it is going to write your input to that file.
So what we are doing here is we're opening the file analyzing realizing that file with the name is yellow
again and we are calling that right method with this input that is into our from the user.
So let's save this first.
Press 5 and it is saying what you love with the python.
So as it is easy to learn OK it is easy to learn.
So when as I said inter I think it should create us a text file and you can see here it is saying fun
let me open this and it is giving us it is easy to learn.
OK so we have a right to that file.
Let's go back to our ideally and now we want to give another value so known.
If I said this again if I press your five again it is saying what you love it the python it these
powerful.
So when it just hit enter this go back again to the fun.
Now we had given that value.
It is easy to learn has been replaced with.
It is powerful.
So we don't need that.
We have to do something like appending techniques so instead of right.
If I just pass it append it is going to happen.
The basically what value you have passed in the previous.
And it is going through this concatenated.
So let me see go again back here.
Let me say this first let me just close this file and now let me say this presser 5 again and I'm going
to say it is 9 AM type.
Language's place into and let me say go again to the fun.
Let me just open this fun.
You'll see it is powerful full was given in the previous execution and it is appended with the now.
It is dynamically typed language.
You can simply pass that is over here.
So every time this is called you can simply go here and put some separations or something like in weird
double slices in this less than over here which is obviously default.
What you can do this in order to give some spaces over them.
So this is the way to just create the file.
So let's remove this now and let's see how to read from the file.
We have already created the file.
Now let's see how to read that file we open we have to open the file so it should be the same name which
we have just created.
It should be fun dot the extreme.
And now the more do will be read more.
And I want to allies it is enough.
And now I have to read with this read command.
So it is pretty much easy but you have to print it.
So I had this print if dot read come on and let me say save this first of five.
It is going to give us the result which is the text inside that file which is it is powerful it is dynamically
typed language which was the file text or content.
So this is the file handling technique first of all you have to open that file if you're done to use
this with.
You have to close that file but it is handled dynamically by this with command.
So we're opening that file in the read write or spend more we're realizing that and we are just calling
this added in the right method to write to that file or read from the file.
So this is the simple file handling technique in the python.
We'll learn more about this in the upcoming lecture.
We'll see some of these technique and how this can be used for the hacking proposed in the upcoming
lectures and the next one.