1
00:00:00,150 --> 00:00:08,010
I have a new report here and inside this report I have from my my usual mainland's profile and also

2
00:00:08,010 --> 00:00:17,280
a directory files, which if I expand, you'll see it has two other directories December and November.

3
00:00:17,610 --> 00:00:24,540
If I expands December, you'll see we have two files inside December and we also have two other files

4
00:00:24,540 --> 00:00:25,650
inside November.

5
00:00:26,160 --> 00:00:26,490
No.

6
00:00:26,610 --> 00:00:34,440
In this video, what we will do is we will write a script which re names all these files that are located

7
00:00:34,500 --> 00:00:37,790
inside these December and November folders.

8
00:00:37,800 --> 00:00:39,810
So the outputs will be.

9
00:00:41,110 --> 00:00:48,000
Something like this for the first file, and we would have something like December as the new file name

10
00:00:48,020 --> 00:00:48,440
J.

11
00:00:48,460 --> 00:00:57,070
H I dot 60, and then we would have December Dash Jake Ale that you see and then we would have November

12
00:00:57,550 --> 00:00:59,890
Dash, ABC, the THC and so on.

13
00:01:00,880 --> 00:01:09,970
So the idea is that we are renaming the files by taking the name of the folder and adding it in front

14
00:01:09,970 --> 00:01:11,470
of each of the files.

15
00:01:11,890 --> 00:01:14,200
How can we do that in Python?

16
00:01:14,470 --> 00:01:17,980
Well, we need the path class for that.

17
00:01:18,400 --> 00:01:27,190
And so again, we create a root directory, which is a path object, an instance of the path class.

18
00:01:27,460 --> 00:01:29,560
So files is this folder.

19
00:01:29,680 --> 00:01:35,440
This contains all the files and folders, so we will be operating on.

20
00:01:35,770 --> 00:01:38,850
So it makes sense to have a directory as a path.

21
00:01:38,950 --> 00:01:48,760
And then next week creates a generator, which is basically a list of all the items inside files you're

22
00:01:48,760 --> 00:01:49,390
going to see.

23
00:01:49,390 --> 00:01:55,330
When I print this out, you're going to see what these file paths contains this time.

24
00:01:55,340 --> 00:01:59,590
So here I am, iterating over file paths, file paths.

25
00:01:59,980 --> 00:02:07,990
And for now, let's print out the path just to orientate yourself to see what we're doing so far.

26
00:02:08,320 --> 00:02:14,770
So our path was printed out two times files slash November file slash Dec.

27
00:02:15,460 --> 00:02:19,270
So inside files, we have two folders December and November.

28
00:02:19,690 --> 00:02:26,380
That's why we got this ultimate now to get access to the files itself.

29
00:02:26,830 --> 00:02:28,780
You have two options.

30
00:02:29,200 --> 00:02:39,520
The first option is to iterate now for file path and path, because path in the first iteration path

31
00:02:39,520 --> 00:02:40,660
is this folder.

32
00:02:41,440 --> 00:02:43,150
Actually, you do that.

33
00:02:43,230 --> 00:02:44,050
It'll do.

34
00:02:45,960 --> 00:02:54,990
So that would give us a list of files inside the current folder, which is November, so that gives

35
00:02:54,990 --> 00:02:58,320
you the list of files inside November and then you print.

36
00:02:58,500 --> 00:03:00,810
Actually, we do something else, but let's.

37
00:03:01,900 --> 00:03:04,600
Putting the out just to see what we have.

38
00:03:04,870 --> 00:03:08,410
Yeah, so now we got access to each of those files.

39
00:03:08,720 --> 00:03:09,040
Right.

40
00:03:09,250 --> 00:03:12,250
And then we can keep on renaming them.

41
00:03:12,580 --> 00:03:22,780
However, there is another more elegant way to do this, and that is by using globe instead of it to

42
00:03:22,780 --> 00:03:23,880
do so.

43
00:03:24,100 --> 00:03:31,570
What you do is you create the file paths using Globe and then here you insert a pattern.

44
00:03:32,350 --> 00:03:40,630
That's and that's which basically means that you are instructing Python to go inside subfolders.

45
00:03:41,110 --> 00:03:44,260
So see now what we will get from that.

46
00:03:44,650 --> 00:03:47,040
In that case, we don't need that second loop.

47
00:03:47,050 --> 00:03:53,650
We don't even the nested loop, because see if you print the path of this file paths, which we get

48
00:03:53,650 --> 00:04:03,580
from here from globe, we get everything that is inside files, including the first level items, which

49
00:04:03,580 --> 00:04:11,350
are December and November, but also whatever is inside all the sub folders also.

50
00:04:11,590 --> 00:04:19,480
So we get ABC not to exceed the TSC, et cetera, et cetera, whatever is inside files.

51
00:04:19,720 --> 00:04:24,580
No matter how deep it is inside directories, it is printed out in here.

52
00:04:25,090 --> 00:04:27,040
So that makes our job easier.

53
00:04:27,280 --> 00:04:35,530
We don't have to do nested loops, but what we do now is, first of all, check if path.

54
00:04:36,700 --> 00:04:37,150
Is.

55
00:04:38,140 --> 00:04:40,750
File, you see these methods here.

56
00:04:41,320 --> 00:04:42,400
You call that.

57
00:04:42,880 --> 00:04:51,290
And then if you print path and execute again, this time you're going to get only the file types.

58
00:04:51,350 --> 00:04:58,150
Not only those items which are files, so not folders this time because this is filtering that out.

59
00:04:58,630 --> 00:05:02,800
So it's file the returns true if the current item is a file.

60
00:05:03,190 --> 00:05:06,670
Therefore, this is true and this is executed.

61
00:05:07,240 --> 00:05:07,540
So.

62
00:05:08,950 --> 00:05:17,350
Then once we grab those files, you want to construct the new file name as a string first and then we

63
00:05:17,350 --> 00:05:19,510
put it in a path.

64
00:05:19,750 --> 00:05:21,850
So a new file name is equal to.

65
00:05:22,870 --> 00:05:27,850
So we said that we want to add in front of ABC.

66
00:05:27,850 --> 00:05:29,500
We want to add in November.

67
00:05:29,680 --> 00:05:30,070
Right.

68
00:05:30,940 --> 00:05:38,190
So the output would be file slash November Dash ABC dot tea.

69
00:05:38,890 --> 00:05:41,500
But where do we get November from?

70
00:05:42,160 --> 00:05:48,250
So we want to get the name of the folder in which the current file is.

71
00:05:48,760 --> 00:05:50,890
And you can get that using.

72
00:05:53,170 --> 00:05:55,980
Path built ports.

73
00:05:56,440 --> 00:05:58,540
See what this outputs.

74
00:05:59,010 --> 00:06:06,400
So Print Patterns folder is the variable I chose for this pen folder is equal to path dot parts.

75
00:06:06,940 --> 00:06:13,030
So let's run this and this is what Ports gives us.

76
00:06:13,600 --> 00:06:20,380
So it gives us the path basically, but split into different items.

77
00:06:20,890 --> 00:06:24,700
So each item represents one a level in the path.

78
00:06:24,970 --> 00:06:30,520
So we have the file first, the file name, and then we have the folder where this file is.

79
00:06:30,760 --> 00:06:34,600
And then we have the other folder where this folder is and so on.

80
00:06:35,080 --> 00:06:44,200
And if we get the item within the X minus two, so that's item because this is a typical as you see

81
00:06:44,200 --> 00:06:45,010
with parentheses.

82
00:06:45,020 --> 00:06:51,040
So it has an index of minus one in here, minus two in here, minus three.

83
00:06:51,070 --> 00:06:59,170
And we know that the item we've indexed minus one is always the folder of the last item and the last

84
00:06:59,170 --> 00:07:00,070
item is the file.

85
00:07:00,250 --> 00:07:02,980
So the folder is minus two.

86
00:07:03,340 --> 00:07:03,760
That's one.

87
00:07:04,030 --> 00:07:05,080
So now we should get.

88
00:07:06,160 --> 00:07:09,580
November, November, December, December.

89
00:07:10,620 --> 00:07:15,210
And then we make use of that in our new file names.

90
00:07:16,080 --> 00:07:19,380
We see patterns folder, which is a string.

91
00:07:19,950 --> 00:07:21,000
So that is a string.

92
00:07:21,330 --> 00:07:23,250
Plus we wanted to dash.

93
00:07:24,000 --> 00:07:26,040
So November Dash.

94
00:07:27,380 --> 00:07:30,580
Path, but name all rights.

95
00:07:30,650 --> 00:07:31,850
I think that should do it.

96
00:07:32,990 --> 00:07:35,600
I'm sure that will do it to run.

97
00:07:37,960 --> 00:07:42,880
I didn't do it because I forgot to add all these three strings together.

98
00:07:43,090 --> 00:07:43,630
That's one.

99
00:07:43,990 --> 00:07:44,590
That's one.

100
00:07:44,980 --> 00:07:45,580
And that's one.

101
00:07:45,760 --> 00:07:49,090
You could also use an F-16, but I don't think that's necessary.

102
00:07:49,210 --> 00:07:56,110
I think this is more readable than using a string of strings are more for a more complex string constructions.

103
00:07:56,620 --> 00:07:59,050
And some, this will be our new file names.

104
00:07:59,350 --> 00:08:02,050
And that's what we want it, says Phahlane's writes.

105
00:08:02,320 --> 00:08:04,150
But it's just a string for now.

106
00:08:04,300 --> 00:08:08,290
So let's construct a path and then we rename that path.

107
00:08:08,950 --> 00:08:14,590
So the new file path begins to path, and that's worth name.

108
00:08:15,010 --> 00:08:18,460
The name should be a string, so a new file name.

109
00:08:18,760 --> 00:08:22,450
So the new file path will be that path, but with a new name.

110
00:08:23,260 --> 00:08:27,650
So basically, we are creating a new path object in here.

111
00:08:27,840 --> 00:08:28,210
Right?

112
00:08:28,810 --> 00:08:30,100
A new path object.

113
00:08:30,520 --> 00:08:33,370
And then we apply rename.

114
00:08:36,200 --> 00:08:38,970
To the path object, that's one.

115
00:08:39,380 --> 00:08:42,020
And insert your new file path.

116
00:08:42,530 --> 00:08:44,030
So it's a path object.

117
00:08:44,330 --> 00:08:45,740
And this is a path object.

118
00:08:45,740 --> 00:08:49,640
Also, don't confuse this with that string object.

119
00:08:50,270 --> 00:08:55,310
So run and the files should have been renamed snow.

120
00:08:55,550 --> 00:08:57,160
So you see this one?

121
00:08:57,170 --> 00:09:01,280
That's that's and that's and that was about this script.

122
00:09:01,550 --> 00:09:08,900
I've been using this script myself because when I create videos, they are split in different sections,

123
00:09:09,200 --> 00:09:13,340
just like we have this December, November here, I have different sections for different parts of the

124
00:09:13,340 --> 00:09:13,610
course.

125
00:09:13,930 --> 00:09:20,990
And then first, I may rename them using lecture numbers, but then I upload them somewhere and I want

126
00:09:20,990 --> 00:09:22,340
more unique names.

127
00:09:22,340 --> 00:09:32,000
So I have to add the section name to each video file to get unique file names for uploading them to

128
00:09:32,000 --> 00:09:33,260
cloud, for example.

129
00:09:33,650 --> 00:09:36,060
So thank you for following this, and I'll talk to you later.

130
00:09:36,260 --> 00:09:36,360
You.

