1
00:00:00,210 --> 00:00:09,390
In this video, we will write a script which redeems all the files inside the files directory.

2
00:00:10,480 --> 00:00:14,930
Specifically, we will adds a prefix to all these files.

3
00:00:15,280 --> 00:00:24,150
So by the end of this video, these files will have been renamed to something like New Dash A, B,

4
00:00:24,160 --> 00:00:28,930
C, C, New Dash D, F, the taxi, etc..

5
00:00:30,430 --> 00:00:37,780
If you follow the previous video and you should have done that, then you know that we will be using

6
00:00:38,050 --> 00:00:42,490
path lib, specifically the path class of path lib.

7
00:00:43,930 --> 00:00:46,390
So how do you go about implementing this?

8
00:00:46,630 --> 00:00:49,180
How do you rename all these files?

9
00:00:49,630 --> 00:00:56,710
Well, whenever you have to manipulate files or directories, change them, rename them, copy them,

10
00:00:56,710 --> 00:00:58,300
delete them first.

11
00:00:58,300 --> 00:01:05,129
You want to determine where these files are in Walt's folder.

12
00:01:05,140 --> 00:01:06,760
What is the root directory?

13
00:01:07,540 --> 00:01:10,420
So in our case, what is the root directory?

14
00:01:10,930 --> 00:01:16,000
Well, our files are inside these files directory.

15
00:01:16,300 --> 00:01:22,260
Therefore, the root directory, which is a variable that I creates, is path.

16
00:01:22,270 --> 00:01:27,640
So I'm creating a path object instance with files as a string.

17
00:01:28,570 --> 00:01:37,060
So with that, now I have the files directory as a path object and I can go further and create.

18
00:01:40,610 --> 00:01:50,150
A lease or file paths using routes, they're dot, it's dear methods with parentheses.

19
00:01:50,540 --> 00:01:53,990
And just to demonstrate you what we have this far.

20
00:01:54,260 --> 00:02:01,490
I'm going to convert this file paths generator into a list because you know that from the previous video

21
00:02:01,490 --> 00:02:07,730
that these methods outputs a generator, so least file paths.

22
00:02:08,120 --> 00:02:10,310
And let's see what we have.

23
00:02:11,050 --> 00:02:11,260
Yeah.

24
00:02:11,270 --> 00:02:16,310
So we have this list of all the files that we want to rename.

25
00:02:18,080 --> 00:02:18,440
Right.

26
00:02:19,640 --> 00:02:23,780
Three files so I can go ahead and delete that print function.

27
00:02:24,600 --> 00:02:27,960
And then guess, what should we do now?

28
00:02:28,710 --> 00:02:34,710
Well, you guessed it, it's a full move, so full path in file paths.

29
00:02:35,130 --> 00:02:38,640
So we will go through each of these three five paths.

30
00:02:39,150 --> 00:02:41,550
The first one, the second one and so on.

31
00:02:42,120 --> 00:02:45,600
And what do we want to do in each iteration?

32
00:02:46,650 --> 00:02:51,510
So our goal again is to rename each of these files.

33
00:02:52,290 --> 00:02:55,680
Therefore, I think we should first think of a new name.

34
00:02:56,680 --> 00:03:05,080
So to construct is trying to represent the name as a string of can we construct a path that points to

35
00:03:05,080 --> 00:03:06,820
that's new file name.

36
00:03:07,300 --> 00:03:09,160
So that's new file, actually.

37
00:03:10,300 --> 00:03:18,880
So I file, for example, this file, it's a file on disk and in Python, this file is represented through

38
00:03:18,880 --> 00:03:24,640
a path file path, and this path object is represented through a string.

39
00:03:24,850 --> 00:03:27,760
So there's a chain of objects, right?

40
00:03:28,510 --> 00:03:36,910
So the new file name is going to be new dash plus path dot.

41
00:03:37,980 --> 00:03:48,060
STEM plus path, dot suffix, let's print file name, new file link to just see what we have.

42
00:03:49,750 --> 00:03:58,100
Run and this is what I intended to have as the new file names, but of course, don't expect these to

43
00:03:58,100 --> 00:04:03,410
be changed by now because we didn't manipulate any paths, right?

44
00:04:03,950 --> 00:04:10,040
We're simply iterating here, constructing a string of than we need to do something with that string.

45
00:04:10,250 --> 00:04:14,540
So let's delete print the way this works.

46
00:04:14,540 --> 00:04:15,670
I think it's clear.

47
00:04:15,680 --> 00:04:18,540
So we have a strong new path.

48
00:04:18,560 --> 00:04:23,720
The stamp gives you only the file name without the extension.

49
00:04:23,720 --> 00:04:24,500
So ABC.

50
00:04:24,650 --> 00:04:28,380
So we have new Dash ABC that takes you.

51
00:04:28,400 --> 00:04:33,140
This gives you the extension of the file name for each file.

52
00:04:34,230 --> 00:04:34,560
Right.

53
00:04:34,950 --> 00:04:41,820
So we said now we need a path or five path, whatever you like to call it.

54
00:04:42,300 --> 00:04:43,590
So I tend to name.

55
00:04:44,650 --> 00:04:54,580
The valuables that points to path objects using paths at the end, some paths here or path here, and

56
00:04:54,580 --> 00:04:58,060
you see here this is a string, so I named it differently.

57
00:05:00,040 --> 00:05:03,430
So a new file path, we said it's a path object.

58
00:05:03,670 --> 00:05:03,990
Right?

59
00:05:04,330 --> 00:05:13,300
And the argument is the string, so path classes get string as arguments.

60
00:05:13,480 --> 00:05:13,810
Right.

61
00:05:14,860 --> 00:05:16,090
In this case, it's new.

62
00:05:19,510 --> 00:05:26,350
File name a, but we need to add something else here, so it's not exactly new file name.

63
00:05:26,800 --> 00:05:31,510
This will cause us some problems and I want to show you this problem now.

64
00:05:31,720 --> 00:05:38,650
So that's you learn a very important points related to manipulating files and folders.

65
00:05:40,270 --> 00:05:45,130
Because see what happens now if I try to rename.

66
00:05:47,270 --> 00:05:57,260
The path, so path is this one in here, which means it is the current path of the loop.

67
00:05:57,500 --> 00:06:09,360
So one of those here and if I rename that path by giving it the new path as argument, so rename it

68
00:06:09,380 --> 00:06:11,840
the methods of a path object.

69
00:06:12,020 --> 00:06:15,120
And it gets another path object as outputs.

70
00:06:15,770 --> 00:06:21,770
If I run this group now, you're going to see some maybe strange outputs, but it has an explanation.

71
00:06:22,040 --> 00:06:27,500
So what happens is that the files are not inside's files anymore.

72
00:06:27,500 --> 00:06:29,090
You see, files is empty.

73
00:06:29,690 --> 00:06:35,370
If I collapse the folder, these files now are outside the files path.

74
00:06:35,450 --> 00:06:39,710
They are in the main or EPFL directory, not inside files.

75
00:06:40,070 --> 00:06:47,960
So what happens is that this new file path, what it is, is this a printed for you?

76
00:06:48,500 --> 00:06:53,060
Print new file path run.

77
00:06:54,810 --> 00:06:57,960
No outputs, because now files is empty.

78
00:06:58,200 --> 00:07:06,390
But let me drag them inside files again as they were before and run the script without renaming.

79
00:07:06,990 --> 00:07:13,530
So Ron, this is the output we get from printing out the file path.

80
00:07:15,140 --> 00:07:22,700
But because the current working directory is set to the rebel directory.

81
00:07:23,960 --> 00:07:26,480
And not to the files directory.

82
00:07:26,990 --> 00:07:30,440
These files are read something that's readable directory.

83
00:07:30,500 --> 00:07:40,430
So the current working directory, you see, that's if I print out print path, that's c w d and call

84
00:07:40,430 --> 00:07:42,530
that method and run.

85
00:07:43,760 --> 00:07:49,910
This is a current working directory where Python is working on of the moment in this script.

86
00:07:50,390 --> 00:07:51,620
And that means.

87
00:07:52,640 --> 00:07:58,490
This new path will be appended to that working directory.

88
00:07:58,670 --> 00:08:05,110
So the complete path of this new file path will be something like home run.

89
00:08:05,230 --> 00:08:11,410
Rename all files to slash new new ABC books.

90
00:08:12,080 --> 00:08:19,850
That's why we see those files written in the main directory, which is rename all files to.

91
00:08:20,190 --> 00:08:24,320
To change that, you need to follow a different approach.

92
00:08:24,980 --> 00:08:38,210
So instead of doing this here, we point to the existing path dot with name and the name is new file

93
00:08:38,210 --> 00:08:38,600
name.

94
00:08:40,289 --> 00:08:49,080
If I run this script now without executing rename just yet, if I run this, this time we'll see files

95
00:08:49,080 --> 00:08:51,410
is appended before these.

96
00:08:51,730 --> 00:08:58,050
Therefore, this file named this file path will be appended to this and therefore we're going to see

97
00:08:58,050 --> 00:08:59,260
the correct outputs.

98
00:08:59,340 --> 00:09:03,810
So a new text is added to the existing was right.

99
00:09:04,390 --> 00:09:10,110
Just to be clear, rename this to the original was.

100
00:09:13,210 --> 00:09:16,510
Like that's and uncomment very name.

101
00:09:17,490 --> 00:09:21,960
Methods so new file name your new file path in here.

102
00:09:22,320 --> 00:09:26,280
New file path in rename and run.

103
00:09:27,900 --> 00:09:31,920
And these are the three renamed files.

104
00:09:32,640 --> 00:09:34,350
And this is the correct altitude.

105
00:09:35,220 --> 00:09:40,980
So that's was about this video there are a few key points to take from this video.

106
00:09:41,190 --> 00:09:45,690
So the first one was that you want to work with path objects.

107
00:09:46,380 --> 00:09:52,890
So if you have a string that represents a file path in disc, then you need to create a path object

108
00:09:52,890 --> 00:10:00,150
type using that string and then be careful with current working directory of the current working directory

109
00:10:00,450 --> 00:10:09,270
is always where your main supply file is located and Main does profile is located in this directory.

110
00:10:09,660 --> 00:10:13,740
Therefore, if you want to write in, your files are already name files.

111
00:10:14,250 --> 00:10:18,120
You want to make sure that the new file path that you're already naming.

112
00:10:19,570 --> 00:10:21,550
Has the correct path.

113
00:10:21,760 --> 00:10:30,820
And in our case, the correct path was files slash new Dash, A, B, C, D, S T and so on for the

114
00:10:30,820 --> 00:10:31,480
other files.

115
00:10:32,290 --> 00:10:34,990
And we did the trick with this part here.

116
00:10:35,290 --> 00:10:44,110
So path with name, that means that we created a new path here from the existing path, but with a new

117
00:10:44,110 --> 00:10:44,620
name.

118
00:10:44,650 --> 00:10:51,490
So basically, this says give me a better path with a new name for the file port, and we set a new

119
00:10:51,490 --> 00:10:55,450
name in here and then we renamed the existing path.

120
00:10:57,020 --> 00:11:00,510
Giving vets new name, I hope that it's clear, and I'll talk to you later.

121
00:11:00,530 --> 00:11:00,980
Thank you.

