1
00:00:00,060 --> 00:00:08,039
Hi, welcome back in this video, you learn how to write a script which merges the contents of all the

2
00:00:08,039 --> 00:00:09,900
text files within a folder.

3
00:00:10,770 --> 00:00:19,290
So for example, we have these files in the files folder, and the outputs of this script will be one

4
00:00:19,290 --> 00:00:22,350
single file containing this content.

5
00:00:22,860 --> 00:00:28,230
And then the next contents after that and then the next after that and so on.

6
00:00:28,410 --> 00:00:30,660
So the output will look something like this.

7
00:00:31,540 --> 00:00:35,910
There's the contents of the first file, then the content of the second and third and so on.

8
00:00:37,230 --> 00:00:40,200
So that is what we will do.

9
00:00:40,860 --> 00:00:46,260
And the way we will do that is by iterating over the files.

10
00:00:46,590 --> 00:00:50,450
Using this for file barfing files, do it to.

11
00:00:50,970 --> 00:01:00,990
So we create a path object representing the directory where the files are located and then iterate over

12
00:01:00,990 --> 00:01:04,560
that files do object.

13
00:01:04,680 --> 00:01:11,780
So this one here using the ITR zero method and in each iteration, then we open the file path.

14
00:01:11,790 --> 00:01:14,670
So that's first file path file for that.

15
00:01:14,670 --> 00:01:18,720
See, as we open, it's in rights mode as this variable.

16
00:01:18,930 --> 00:01:26,430
And then we read the contents of that first file folder, see as we file and store that content in the

17
00:01:26,430 --> 00:01:27,420
contents variable.

18
00:01:28,980 --> 00:01:31,710
But then what do we do with this content?

19
00:01:32,010 --> 00:01:39,690
Because now we go to the next iteration, writes File for the CAC File five this year.

20
00:01:40,260 --> 00:01:50,580
So what we want to do is we want to have perhaps an empty string in here, then in each iteration.

21
00:01:50,820 --> 00:02:03,210
You want to exit that with block and say merge equals to merge plus contents.

22
00:02:04,050 --> 00:02:11,530
So what we're doing basically is, let's say we have this merged empty string right now.

23
00:02:11,550 --> 00:02:22,950
If you do merge the Eagle to merge, plus some new content here, a new string merge gets updated to

24
00:02:22,950 --> 00:02:24,150
that new content.

25
00:02:24,600 --> 00:02:33,660
So let's say this was the first iteration of then in the next iteration, we again, Python again executes

26
00:02:33,660 --> 00:02:33,960
this.

27
00:02:34,170 --> 00:02:42,330
So that means Merge is again able to merge, plus some more content.

28
00:02:42,990 --> 00:02:49,290
And what we're doing here is we are concatenating that's new contents string.

29
00:02:49,530 --> 00:02:55,950
We've that some more content string and what we end up having is new content.

30
00:02:57,300 --> 00:03:03,690
Plus, some more content now, you see, we don't have a space here, so to improve, that's what we

31
00:03:03,690 --> 00:03:04,560
want to do is we.

32
00:03:05,280 --> 00:03:15,430
Plus backslash and here we add the break line between these two and that's we'll fix it right on Sue.

33
00:03:15,530 --> 00:03:25,680
Once we construct that string in the loop, then we exit the loop and let's sprint merge to see once

34
00:03:25,680 --> 00:03:26,130
we get.

35
00:03:27,380 --> 00:03:33,560
Yeah, so we have the concatenated string that is the first content of the first file, that is the

36
00:03:33,560 --> 00:03:37,850
second content of the second file and that is the third content of the third file.

37
00:03:38,470 --> 00:03:46,490
And so instead of printing it out, of course, we want to open a new merge the X, T or C as v file

38
00:03:47,330 --> 00:03:54,550
in rights mode s file and file that rights merge goes there.

39
00:03:54,560 --> 00:03:59,600
The variable that has this thing run, this is the output.

40
00:04:00,940 --> 00:04:06,280
And that is how you merge or concatenate text, file, the contents of text files.

41
00:04:06,550 --> 00:04:08,380
Now we have a problem here.

42
00:04:08,380 --> 00:04:09,670
Of course, you can notice that.

43
00:04:10,410 --> 00:04:14,950
Well, this is repeated in every file.

44
00:04:15,070 --> 00:04:22,870
So perhaps in this scenario, we want to avoid having that in every X number of roles.

45
00:04:23,200 --> 00:04:30,460
So how do we go about having this only once and removing it from here?

46
00:04:30,790 --> 00:04:32,950
So not that's not that.

47
00:04:33,280 --> 00:04:34,870
Let's do that in the next video.

48
00:04:35,020 --> 00:04:35,560
See you there.

