Hi, welcome back in this video, you learn how to write a script which merges the contents of all the
text files within a folder.
So for example, we have these files in the files folder, and the outputs of this script will be one
single file containing this content.
And then the next contents after that and then the next after that and so on.
So the output will look something like this.
There's the contents of the first file, then the content of the second and third and so on.
So that is what we will do.
And the way we will do that is by iterating over the files.
Using this for file barfing files, do it to.
So we create a path object representing the directory where the files are located and then iterate over
that files do object.
So this one here using the ITR zero method and in each iteration, then we open the file path.
So that's first file path file for that.
See, as we open, it's in rights mode as this variable.
And then we read the contents of that first file folder, see as we file and store that content in the
contents variable.
But then what do we do with this content?
Because now we go to the next iteration, writes File for the CAC File five this year.
So what we want to do is we want to have perhaps an empty string in here, then in each iteration.
You want to exit that with block and say merge equals to merge plus contents.
So what we're doing basically is, let's say we have this merged empty string right now.
If you do merge the Eagle to merge, plus some new content here, a new string merge gets updated to
that new content.
So let's say this was the first iteration of then in the next iteration, we again, Python again executes
this.
So that means Merge is again able to merge, plus some more content.
And what we're doing here is we are concatenating that's new contents string.
We've that some more content string and what we end up having is new content.
Plus, some more content now, you see, we don't have a space here, so to improve, that's what we
want to do is we.
Plus backslash and here we add the break line between these two and that's we'll fix it right on Sue.
Once we construct that string in the loop, then we exit the loop and let's sprint merge to see once
we get.
Yeah, so we have the concatenated string that is the first content of the first file, that is the
second content of the second file and that is the third content of the third file.
And so instead of printing it out, of course, we want to open a new merge the X, T or C as v file
in rights mode s file and file that rights merge goes there.
The variable that has this thing run, this is the output.
And that is how you merge or concatenate text, file, the contents of text files.
Now we have a problem here.
Of course, you can notice that.
Well, this is repeated in every file.
So perhaps in this scenario, we want to avoid having that in every X number of roles.
So how do we go about having this only once and removing it from here?
So not that's not that.
Let's do that in the next video.
See you there.