Hello guys.
So we are going to continue the discussion with respect to Python.
And in this video we are going to talk about file operations.
Uh, we will be performing various file operations with the help of Python programming language.
Uh, we will be playing with both text and binary files.
And we will also be understanding how to properly create files, how to write inside that, how to read
the file content and many more things.
So let us go ahead.
And first of all, let me just go ahead and do the first task that is read a whole file.
Right.
So for reading a whole file again we will be using with open method.
Right.
And here I am going to give my file name.
Let's say that I'm going to give example dot txt and the first second parameter inside this open you
know we will specifically use this r parameter r basically means uh read okay.
And they have also other parameters which we will discuss like write write, append read append append
write.
Sorry not read append.
It is append separately.
Then we have w plus operation where we can both read and write a specific file.
So we'll be seeing one by one.
So let us first of all start with read.
So this will be my read operation.
And I will create a object of file.
And then from line in line okay.
So from line in file.
So inside this particular file how many lines are there like in example dot txt will be keep on reading
it.
And now let me do one thing instead of writing.
If instead of just going line to line initially, we'll just go ahead and create a variable called as
content and we'll take this file.
And then there is operation which is called as dot read.
So what dot read is basically going to do is that it is just going to read the entire content of the
file.
Okay.
So right now I have not created the file that is example dot txt.
So we will go ahead and execute it.
So here you can see that I am getting an error saying that hey no such file or directory in example
dot txt right.
So this is the first error we usually get whenever we are directly trying to read.
I'll be showing you other ways also, but there what will happen is that if I'm trying to read it right,
first of all, it will create a file and then we will be able to read it.
Okay.
Now here what I'm going to do.
So this is the file handling part I will go ahead and create one example dot txt file okay.
And with respect to this example dot txt file I'll just go ahead and write.
Hello how are you.
Okay.
And I am good okay.
Something like this crush is my name okay.
And welcome to the course okay.
Something like this.
So now I'm going to perform all these things.
I'll be reading specifically from this example dot txt.
And now let me just go ahead and execute it okay.
You'll be able to see that.
Now I'm reading the entire content right.
So with example dot txt this is basically opening this particular file in the read mode.
Okay.
Will not be able to write if we are opening with the help of read mode.
Right.
And then we are just reading the entire file content.
Now I may also have a scenario that I want to read a file line by line.
Okay, I just want to read it line by line.
Now, how do I read it?
Line by line.
So here we are going to specifically use with open operator.
Right here I'm going to use my again example dot txt file.
And this will specifically open in read mode.
And now I'm going to use this particular object as file.
Now since I have to read all these things line by line.
So now I can go ahead and use my for loops you know, so that file will be uh, over here as an iterable
because I have so much of content over here.
Right.
So for line in file.
Right.
I'm just going to go ahead and print my line.
Okay.
And uh, what I'm actually also going to do is that if I go ahead and execute this.
So here you can see example okay.
Example spelling is not right.
So I'll just make it right now here you can see that when I'm reading line by line it is also taking
the new line characters.
Right.
So here because when I see this right if I go to the next line.
So here a new line is basically getting added.
So that is the reason you'll be able to see this space.
Now let's say I want to go ahead and remove this particular new line character.
So for that I'll be using an operation which is called as dot strip.
Okay.
So this dot strip, uh, this dot strip operation removes the new line character.
Perfect.
So this is what we are able to do it out of it okay.
So this is what is there.
Right.
And we are able to get the output.
Now let's do some more operation.
Now what I'm actually going to do over here is that uh, uh, let's say that I want to probably, uh,
open a file for writing something inside it.
Right.
So here, let me go ahead and write writing a file.
Okay.
And here I will also make sure that I will show you over writing classes.
Okay.
Over here right now let me do the same thing with open Function.
And I'm going to basically open this.
And here I'm going to use example dot txt right.
And this will basically be my write right byte mode and as file.
Okay.
And then I will go ahead and write file dot.
Right.
And let me quickly go ahead and write hello world.
Okay I'm writing this hello world with a new line character.
Okay so I dot write right is a method which will specifically help me to write anything inside this
file.
And here you can see that I'm using W mode over here.
W mode is nothing but write mode.
Okay.
Now this is the first line that I'm going to add over there with a new line.
And then I'm going to add file dot write.
This is again a new line.
So let's say I'll just go ahead and write.
This is a new line.
Okay.
And let me just go ahead and execute this.
So let's see uh what will basically happen now as soon as I execute this already.
You know in example dot txt I have this content.
So if I go ahead and execute it here, you will be able to see that the entire content got overwritten.
Right.
So overwritten basically because I have just opened this particular file for the right mode and it will
just consider, okay, the file is completely empty.
And when we write file dot right, it is just going to overwrite completely from this particular content.
And again this is a problem out there.
You know we should never be doing like this.
Instead uh, if I want to write a file, write a file, write write a file without overwriting, okay.
Without overwriting, then I will be using a different approach.
So let me just go ahead and do it.
So I will say from open okay.
And here I'm going to specifically use example dot txt.
And let me just open this in another mode which is called as append mode.
Okay.
Now in this append mode what will happen is that if I go ahead and write anything.
So if I go ahead and write file dot write uh append operation taking place.
Append operation taking place.
Okay.
And if I go ahead and execute it now so here you'll be able to see this operation has taken place.
Now if I go ahead and see in the example dot txt, this is the new thing that is getting appended,
right?
I may also want to write it in the next line.
So you can just use slash n over here and do it right.
And now here clearly you can see that I'm able to append any text.
Okay.
Let's say I will go ahead and write slash n again I'll try to append this okay.
So here you'll be able to see again it is getting appended.
Now along with that what I'm actually going to do is that again let's go ahead and append this.
So here you can see it will go to the new line because I had used slash n before.
Right.
So the file operation is basically taking place.
And here you are able to append it.
Okay.
Uh now there may be also scenarios that I want to write right.
Multiple lines okay.
Multiple lines specifically.
So uh, writing a list of lines to a file.
So let's do this okay.
Again uh, let's say this is lines okay.
These are my list of lines.
So here I'm just going to write okay.
This is my first line slash n r.
Then the second line slash n.
Okay.
Um.
And let's go to the third line.
Third line slash n.
Okay.
So these are my three lines that I really want to add.
Okay.
Uh, let me make some cells and let me copy this thing entirely.
Okay.
And let me do this operation over here.
Okay.
Now, what I'm actually going to do, I will just go ahead and open my file.
Open my file.
Okay.
And I will try to open this same example dot txt file in my append mode.
Okay.
In my append mode and I'll create an object as file.
Now in order to write all the lines right inside this particular list, there is again a function which
I can actually use, which is called as file dot write lines and all.
I have to give my list over here, right?
So once I execute it you will be able to see that.
now.
First line, second line and third line has got upended.
Okay.
So, uh, apologies for this particular spelling kind of errors.
So you can actually make the changes as based on your grammatical things that you have.
Right?
So I'm just making sure that I try to show you more amazing features with respect to this.
Okay.
Now this was with respect to text file.
Now let's go ahead and discuss with respect to binary files okay.
How to work with binary file.
Binary files are usually uh if I probably see the extension it is basically a bin file.
Right.
So if I go ahead and create a file over here, you'll be able to see if I go and write example dot bin
okay.
So this is what we basically say it as binary files okay.
Let me just go ahead and append something over here.
Hello world.
Something okay.
Some information over here.
And this will be in the form of binary files.
Now I'll go ahead and show you how you can read or write binary files in binary files we usually update
bytes okay.
Bytes is very much important.
So I'll just keep this as empty right now with respect to binary files.
So let me just go ahead and write.
So here I'm just going to write to a binary file.
I'll be using this kind of bytes okay.
Uh with open example dot bin in right byte mode okay.
So now my this operation will become it will not be read or write.
It will be read byte or write byte.
Okay.
Since I am going to write.
So I'm going to basically use it as write byte.
Now if I go ahead and execute it.
So here you will be able to see in the bin.
This is what I am actually able to get okay in in the form of bytes okay.
So this is one of the operation that you can see over here right.
Similarly if you want to probably read it uh, again we can use instead of writing R we can also use
RB okay.
So RB as file file dot read.
And we are just going to print the content.
And this is what I am able to display right now in bin file.
It is looking like this.
And this is what I think actually uh hex characters for the those same thing what we have actually written.
Okay, so this was one of the most beautiful thing with respect to file operation.
Now, as I said, uh, let's do some of the things, write some, some, some practical examples and
all, uh, how to probably solve it.
Okay.
Uh, let's say, uh, I want to read I want to read the content from a source text file and write it
to a, a destination text file.
Okay.
If I want to do this, I can go ahead and write it again.
For this, it is very much simple.
I will first of all read from a file.
So let's say I am reading from my example dot txt and I'm writing it to a destination dot txt.
So I have not created destination dot txt.
Let's see whether we will get an error or not.
So here is the content that I'm getting.
Source underscore file dot read.
And when I write destination underscore file dot write content Okay, so if I execute it here automatically,
you'll be able to see that I'll be creating this destination dot txt.
And this was my example dot txt okay.
This is one of the operation.
One more uh assignment that you can probably take.
Okay.
And please make sure that you do this uh it will be important.
Okay.
Um, and, uh, read read a text file.
Okay.
And I have already done this, I think, for you all, but I'll give you this as an assignment.
Read a text file and count the number of lines, words and characters.
And this is the code.
So first of all, you try it from your side.
Anyhow, I'll be giving you the code.
Okay.
You can give the file path example dot txt and just go ahead and do this right.
So this is a function that has got created.
We are reading the lines we are.
We are doing the summing up of all the lines itself okay.
So this one assignment you can probably take it.
Okay.
So uh I think we have discussed so many things about it, but uh, let us go ahead and discuss about
one more operation.
Okay.
Uh, that is nothing but writing and reading, writing and then reading a file.
Okay.
This kind of operation.
Okay.
So what I'm actually going to do, I'm going to basically use with open again.
And let me just go ahead and write example dot txt.
Okay.
And this time I'm going to open this file in W plus mode.
W plus mode is nothing.
But if you don't know about W plus mode, I'll just give a statement over here so that you'll be having
this as a markdown.
The W plus mode in Python is used to open a file for both reading and writing.
If the file does not exist, it will be created.
If the file exists, the cut its content is truncated.
That is, the file is overwritten.
Okay, so here you'll be able to see that I'm using w plus mode as file okay.
And here I'm just going to go ahead and probably write the data to the file.
So let me just go ahead and write file dot write.
And here I'm just going to say hey hello World, okay?
It is just going to overwrite it.
Okay.
And then I will go ahead and write file dot.
Right.
Okay.
This is a new line.
Okay.
This is done.
Okay.
Perfect.
Now after we write, uh, we will make sure that we move the file.
Cursor.
Okay.
What?
Right now, the file cursor will be pointing to the second line.
Okay.
It will be pointing to the second line that is over here.
But we need to move the cursor to the first one so that we will be able to read it.
Okay.
So move the file cursor to the beginning.
And how do we do this.
We just use one very important method which is called as file dot seek.
Okay.
Now this seek uh, if you probably go ahead and see this, this if I give it to zero.
So it is just going to move the file cursor to the beginning.
That is just like a zeroth character or zeroth index, right.
Then we can go ahead and read the content of the file.
Right.
And then I'll go ahead and print the content.
Okay.
First of all, I'll show you what will happen if I comment this out.
Okay.
Will it display anything?
Okay.
So let's go ahead and run this.
So here you can see I'm getting hello world.
Uh, this is a new text line.
Uh, okay.
Let's see what is getting printed.
So print content is basically happening over here.
Uh, let me see whether my example dot txt has got loaded.
So example dot txt.
This overloaded uh this is basically giving me a new sentence hello world and all.
And here you will be able to see that I'm printing the content.
Okay.
Uh, just a second.
Okay.
So the thing is that I did not read the file, so previous variable, only it was showing me.
Now, let me do one thing.
Okay.
File dot read.
Okay.
Now, I did not read the file.
Right.
So what I'm actually going to do.
See, without I'm just commenting this.
Okay.
And we are writing this two lines in my example dot txt and we are reading it.
If we do not write this line of code, what will happen?
It will not display us anything.
Okay.
And here you can see an example dot txt.
This two line has got appended because the cursor will be pointing here.
We have to make sure that we have to take the cursor over here right to the first one.
So that is the reason you'll be able to see.
Once I probably remove this.
And if I execute it now, you'll be able to see that I'm actually able to get the both the line.
Okay.
So this is what you'll be able to see with respect to this and how amazing this is, right?
Uh, again, uh, let's say that you want to do multiple times read and write.
Always make sure that you try to use this file dot seek and make it to the beginning one.
Right.
Um, yes.
This was it.
I think I had, uh, probably taken a lot many things with respect to file handling.
Uh, but yes, as we go ahead and see multiple use cases as we go ahead in the upcoming sessions, we'll
be seeing more different kind of operations.
So, yes, this was it from my side.
I will see you all in the next video.
Thank you.
Take care.