Let's suppose a scenario where we have multiple Sears files and each of these files have an extra coma,
which is unwanted, and we want to remove that coma of the ends of each file.
And of course, we want to use Python to do that.
So we want to automate the process instead of going through each file and deleting that it could be
a thousand 2000 files.
You don't want to do that.
It's very easy to do with Python.
So in this video, we're going to remove a comma, the lost character from a C as we file.
But let's slow down a bit and let's work on one single file first.
So I'm going to remove two of these files and just work on one of them in the next video.
Then we will work on multiple files to remove the lost character of all the files.
So see, as we files are just another type of text file, they happen to have a C USB extension, but
you can open them in a text editor.
So these are files that are separated by commas by separating these files by a comma.
You have this well-defined structure to represent data.
So, for example, we have this idea and so we have rows.
34 is the idea for the first item?
Fifty six is the idea for the next slide.
Some five is the idea for the other item and so on.
But then we want to remove this comma.
Let's see how to do that in Python with open.
Of course, we want to use the open methods file three that see us V..
And what do we want to do?
We want to get the contents of the file first.
So we want to open the file in read mode.
As foil and grab the contents with file that reads so we save the contents and the contents variable.
Now see if we got that right.
So on intent and print content.
And yet, so that is the content.
So this is a string, a plane, python string and as you know, with strings, we can do slice operations,
for example, we can get all the characters, but the last one wrong, that's.
And you see that the coma is not there anymore because a string has an indexing system embedded, so
the less character has an index of minus one.
That means the coma has an index of minus one.
And when you do this, you're saying, give me all the items from the item with index zero, which is
I.
So that as an index of zero, this as an index of one to three, four, eight and so on.
And you also have this a negative indexing so minus one minus two, minus three and so on.
So we get a zero, we get one two three and minus one.
So minus one is not included.
You can run this again.
You get the same thing.
So that is the equivalent of that.
So this is considered to be better because you avoid having to ride the zero there.
Of course, if you right minus two there and run.
The lost zero is going to go way.
Also minus three.
Like that and so on, so minus one is what we need, and we want to stall this results in a new variable.
So let's say modified contents eagles to vet content with that slice.
Some modified content now has no comma of the ends.
What do we want to do with the modified content?
Stream now is open a new file.
Now here you have to take a decision.
You either ride the same file name or file that see as V as here.
And that means you will override this file with the new content, which does not have a comma, or you
can create a new file to create a new file.
All you have to do is give a new name to the new file, and that means you will not override the existing
file.
So like, that's in right mode.
S file.
Five, that's right, modify its contents, execute.
And this is a new file without a comma.
And that is the coat.
Let's try by overwriting the file.
File three The case we run.
Now you go to five three doses and you don't see the coma anymore.
Whether you overrides or creates a new file that that is up to you, I would suggest that if you want
to override the existing file, perhaps you want to make a copy first, just copy the folder where your
files are and put it somewhere else just to back it up in case you screw things with Python.
So that was an example on using file reading and writing.
In practical example, in the next video, we will remove the comma from multiple files at once.
So sue there.