1

00:00:00,360  -->  00:00:05,640
Welcome to lesson 34 I have a folder on my hard drive called Delicious.

2

00:00:05,640  -->  00:00:07,980
It's right here in the root folder.

3

00:00:08,310  -->  00:00:10,750
And delicious has some more content inside of it.

4

00:00:10,740  -->  00:00:17,910
It has other folders and files like this foo folder or walnut folder has a file named spam txt and another

5

00:00:17,910  -->  00:00:20,870
one called Spam Spam Spam dirty XTi.

6

00:00:21,000  -->  00:00:28,200
If we go into that walnut folder and see it also has a folder and has an exe that text the file and

7

00:00:28,380  -->  00:00:34,680
inside that Waffles folder or some other files say I went to write some code that would apply to all

8

00:00:34,680  -->  00:00:38,970
of the folders and files inside of this delicious folder.

9

00:00:39,270  -->  00:00:47,010
Basically what I want to do is walk through this folder tree and either rename or copy or do something

10

00:00:47,010  -->  00:00:49,580
with all of these files in that code.

11

00:00:49,980  -->  00:00:52,250
Reading a program to do this could get tricky.

12

00:00:52,260  -->  00:00:58,500
But fortunately Python provides a function to handle this process for you.

13

00:00:58,500  -->  00:01:01,470
It's called the OS walk function.

14

00:01:01,470  -->  00:01:10,560
So first let's import the os module and Os up walk in past a root folder that you want to check.

15

00:01:10,560  -->  00:01:16,680
So in this case I want to look at the delicious folder and all the folders and files underneath it.

16

00:01:16,680  -->  00:01:21,270
Now the return value for this is used in four loops.

17

00:01:21,270  -->  00:01:26,340
I'll just say for right there and it actually instead of just returning one value like what the range

18

00:01:26,340  -->  00:01:30,390
function does so sort of having something like for I in range.

19

00:01:30,480  -->  00:01:35,130
It actually returns three values on each iteration of this loop.

20

00:01:35,220  -->  00:01:36,880
So the three values you can.

21

00:01:36,930  -->  00:01:40,100
This is just a regular for loop so you can name the variables whatever you want.

22

00:01:40,110  -->  00:01:42,310
But I like to go with these names.

23

00:01:42,370  -->  00:01:48,540
The first one I call folder name because the first of the three values will be a string with the folder

24

00:01:48,540  -->  00:01:49,890
name that it's currently looking at.

25

00:01:49,890  -->  00:01:57,210
On this iteration and then it also provides a list of all the folders inside that folder so I'm just

26

00:01:57,210  -->  00:02:03,210
going to say the variable subfolder can contain that list that also contains a list of strings for all

27

00:02:03,210  -->  00:02:07,560
the files inside this folder.

28

00:02:07,560  -->  00:02:11,840
This is kind of like what the OS does list during function returns.

29

00:02:11,850  -->  00:02:18,090
It's a list of file names so I can create this for loop and I'll just have a colon right here and on

30

00:02:18,120  -->  00:02:23,940
each iteration of this loop it'll go through all the folders underneath this delicious folder and on

31

00:02:23,940  -->  00:02:28,770
each iteration it also provides all the folders that are in that folder and all the files that are in

32

00:02:28,770  -->  00:02:30,150
that folder.

33

00:02:30,150  -->  00:02:34,460
So let me just write up some code just to that you can see what exactly is happening here.

34

00:02:34,470  -->  00:02:38,580
You have a lot of print functions here just to display all this data.

35

00:02:38,610  -->  00:02:45,020
I'm going to say the folder is and will just append the folder name.

36

00:02:45,330  -->  00:02:50,380
So on this iteration this or just print out whatever the folder it's currently looking at is and then

37

00:02:50,580  -->  00:03:06,090
also sub folders is a list so I could just say print the sub folders in folder name are and then I'll

38

00:03:06,090  -->  00:03:14,730
just print out this list value to convert that to a string first so that we can concatenate it and we

39

00:03:14,730  -->  00:03:21,080
can do the same thing here with the file names list.

40

00:03:21,120  -->  00:03:25,660
That's also a list others say and the file names inside that folder name are this.

41

00:03:25,860  -->  00:03:30,390
And then just have a blank print call just to print out a new life.

42

00:03:30,420  -->  00:03:36,590
You know when I press Enter this is going to walk through the entire for the entire directory tree or

43

00:03:36,600  -->  00:03:38,430
folder tree underneath.

44

00:03:38,430  -->  00:03:40,130
Delicious.

45

00:03:41,610  -->  00:03:44,550
So here's the output from this for loop.

46

00:03:44,550  -->  00:03:50,100
So here's the first iteration on that iteration is just looking at the delicious folder itself.

47

00:03:50,100  -->  00:03:58,020
And it's also so folder name has been set to see slash delicious and the subfolder list is a list of

48

00:03:58,020  -->  00:04:06,330
all the folder names inside that folder and file names is a list of all the files inside that folder

49

00:04:06,330  -->  00:04:06,720
.

50

00:04:06,720  -->  00:04:10,170
But then here's the great thing on the next iteration of this.

51

00:04:10,200  -->  00:04:11,720
Oh estopped walk.

52

00:04:11,760  -->  00:04:15,880
We're going to examine all of the folders underneath this delicious folder.

53

00:04:15,990  -->  00:04:21,660
So first we're going to take a look at delicious flesh food and I'll tell us all the folders and file

54

00:04:21,660  -->  00:04:26,940
names there and then I'll take a look at delicious slash walnut and I'll tell us all the folders and

55

00:04:26,940  -->  00:04:33,440
file names in that folder and then I'll keep diving deeper and deeper.

56

00:04:33,480  -->  00:04:40,140
Mel looking at delicious lush walnut slash waffles and I'll tell us all the folders and file names there

57

00:04:40,150  -->  00:04:41,070
.

58

00:04:41,600  -->  00:04:48,610
So I'm just going to copy this out and create a new file editor.

59

00:04:48,830  -->  00:04:50,060
We have code like this.

60

00:04:50,220  -->  00:04:56,640
So in each iteration it's easy to add code that would loop over this list of folder names we'd just

61

00:04:56,640  -->  00:05:01,510
say for subfolder in the folders.

62

00:05:01,660  -->  00:05:06,600
Or we could print out the subfolder name or we could do whatever we need to do with it.

63

00:05:06,600  -->  00:05:13,350
We could have code that say deletes this subfolder we want it to go through and delete all of the folders

64

00:05:13,350  -->  00:05:20,790
underneath delicious or if we wanted to rename it but only under some specific circumstance like if

65

00:05:21,090  -->  00:05:26,280
the string fish was in the subfolder name like we had a whole bunch of folders and some of them had

66

00:05:26,280  -->  00:05:32,310
the name fish in them and we wanted to delete all of the fish folders in here I would then say OK in

67

00:05:32,310  -->  00:05:33,510
that case then delete

68

00:05:36,210  -->  00:05:43,410
remove directory subfolder and we could do the same thing with all the files in the file names list

69

00:05:43,410  -->  00:05:43,440
.

70

00:05:43,440  -->  00:05:46,330
So for a file in file names you know.

71

00:05:46,350  -->  00:05:48,090
So if this file

72

00:05:50,640  -->  00:05:57,340
ended with some certain extension like we went to read all the Python source files there.

73

00:05:58,020  -->  00:06:00,780
Or maybe we just want to copy them somewhere else.

74

00:06:00,780  -->  00:06:05,370
We have like a copy function and just add the.

75

00:06:05,820  -->  00:06:12,600
And then add the old name and then you know maybe we want to rename it so that it had a new extension

76

00:06:12,600  -->  00:06:16,670
like back up or something like this.

77

00:06:16,710  -->  00:06:22,220
This is kind of tricky because remember this is just a string of a file name it's not in absolute paths

78

00:06:22,220  -->  00:06:27,600
so we'd have to call the OS to join to join the folder name

79

00:06:31,980  -->  00:06:32,970
and that file

80

00:06:34,660  -->  00:06:37,100
.

81

00:06:38,040  -->  00:06:46,200
So say we wanted to rename it to have done back up at the end of it but using code like this allows

82

00:06:46,200  -->  00:06:52,590
us to do a lot of small decision making on a lot of files thousands of files or folders.

83

00:06:52,590  -->  00:06:57,750
That's really wouldn't be possible to do with the file explorer like you know it's really easy for me

84

00:06:57,750  -->  00:07:04,320
to say oh I want to just delete the entire delicious folder so I could just highlight all of this and

85

00:07:04,320  -->  00:07:06,320
then right click and select Delete.

86

00:07:06,540  -->  00:07:12,900
But say I want to you know find all of those say all these SoBo these folders that had the name fish

87

00:07:12,900  -->  00:07:14,640
in them and just delete those.

88

00:07:14,790  -->  00:07:21,480
Then that's there's no way to just massively do that I'd have to click through every single folder underneath

89

00:07:21,480  -->  00:07:24,330
the delicious directory.

90

00:07:25,320  -->  00:07:27,800
And for this in this case that would be that much.

91

00:07:27,810  -->  00:07:33,030
But say I had a folder with thousands of files and thousands of folders in it that would take up a lot

92

00:07:33,030  -->  00:07:33,760
of time.

93

00:07:33,790  -->  00:07:42,030
So do it using this OS walk fold function allows us to write code where we can perform some sort of

94

00:07:42,030  -->  00:07:49,050
action or run some code against all of the folders or file names underneath a certain folder.

95

00:07:49,050  -->  00:07:55,050
And if I wanted to do this for all of the files on my computer which probably isn't recommended I could

96

00:07:55,050  -->  00:07:59,940
just put in the root folder name is really isn't recommended I should probably do a dry run first by

97

00:07:59,940  -->  00:08:11,120
commenting out this code into printing something like You know I'm removing this directory.

98

00:08:11,490  -->  00:08:17,520
Same thing here where I can print out a message saying I'm copying this file to this new file name

99

00:08:20,370  -->  00:08:24,290
but there's a lot of functionality that you can use in OS start walk.

100

00:08:24,360  -->  00:08:31,050
Just remember you all use Start Wachs return value in a for loop and that returns three different values

101

00:08:31,050  -->  00:08:32,320
on each iteration.

102

00:08:32,520  -->  00:08:38,100
The folder name of the current folder it's looking at all of the subfolder is a list of strings for

103

00:08:38,100  -->  00:08:44,470
the sub folders inside this folder and also a list of strings for the file names inside that folder

104

00:08:44,480  -->  00:08:44,990
.
