1

00:00:00,390  -->  00:00:04,860
Welcome to lesson 30 which goes over the start of Chapter Eight of the automate the boring stuff with

2

00:00:04,860  -->  00:00:09,930
Python textbook in this lesson we'll talk about files and folders and how python can work with them

3

00:00:09,950  -->  00:00:10,230
.

4

00:00:10,500  -->  00:00:15,060
So variables are a fine way to store data when your program is running but just like you want to save

5

00:00:15,060  -->  00:00:18,740
your work in a word processor program before you turn off your computer.

6

00:00:18,810  -->  00:00:23,320
If you want your data to persist after your program is finished you need to save it to a file.

7

00:00:23,460  -->  00:00:27,050
You can think of a file's contents as a single string value.

8

00:00:27,090  -->  00:00:31,700
Of course if a file is gigabytes in size this string will be billions of characters long.

9

00:00:31,830  -->  00:00:36,110
But first we have to learn a few concepts about files.

10

00:00:36,540  -->  00:00:40,080
Here's my desktop which has a whole bunch of files and folders on them.

11

00:00:40,080  -->  00:00:44,220
Folders can contain other folders and files.

12

00:00:44,220  -->  00:00:49,570
So I'll just open up this marble adder file that has a whole bunch of TNG image files here.

13

00:00:49,600  -->  00:00:56,310
Each of these files has two key properties a file name which you can see right here and a path which

14

00:00:56,310  -->  00:00:59,020
is its location on the hard drive.

15

00:00:59,080  -->  00:01:05,100
Seems you right here it's inside the marble adder folder which itself is inside the Desktop folder which

16

00:01:05,100  -->  00:01:11,660
is inside this owl folder which is inside this user folder which is inside this Seacole and folder.

17

00:01:11,820  -->  00:01:14,350
This altogether is called a file path.

18

00:01:14,370  -->  00:01:18,230
It's just a list of folders that a file is inside of.

19

00:01:18,270  -->  00:01:21,100
So we can go to the very top one.

20

00:01:21,350  -->  00:01:24,760
This C-Drive and this is called the root folder.

21

00:01:24,780  -->  00:01:29,020
This is the folder that contains every other folder on the hard drive.

22

00:01:29,040  -->  00:01:31,860
So in Windows this is going to be Seacole and slash.

23

00:01:31,860  -->  00:01:37,320
Whereas on Mac and Linux is just going to be splash and everything that you have to pay attention to

24

00:01:37,590  -->  00:01:44,040
is that on Windows there is a slash separator for all of these folder names and the backslash on Windows

25

00:01:44,040  -->  00:01:44,060
.

26

00:01:44,070  -->  00:01:48,770
But on Linux and Macs it's going to be the forward slash files.

27

00:01:48,780  -->  00:01:53,260
Also have a file extension which is the last part of the file name.

28

00:01:53,430  -->  00:01:57,130
In C it comes after the dot so the extension on this file is.

29

00:01:57,180  -->  00:02:01,290
TNG and the file extension tells you what type of file it is.

30

00:02:01,580  -->  00:02:04,080
So when I double click on this PFG image file.

31

00:02:04,200  -->  00:02:10,220
Windows knows that it should use some sort of image viewing program to display it.

32

00:02:10,710  -->  00:02:12,670
So let's go back to the interactive shell.

33

00:02:12,720  -->  00:02:19,470
I will be using strings to represent file paths and file names in C..

34

00:02:20,050  -->  00:02:27,530
I get to have some sort of fictional file path right here represented as this string.

35

00:02:27,540  -->  00:02:32,280
Notice of course in Python for Windows since we use the backslash the backslash character is also used

36

00:02:32,280  -->  00:02:39,530
for two escape characters such as a newline character will just be backslash in.

37

00:02:39,610  -->  00:02:44,860
If you want to have an actual literal backslash you need to escape the backslash itself.

38

00:02:44,880  -->  00:02:48,060
So if I have something like print out this string value.

39

00:02:48,060  -->  00:02:50,060
This is how I can print out an actual slash.

40

00:02:50,070  -->  00:02:56,610
So if you want slash backslashes in your strings be sure to have two backslashes or begin the string

41

00:02:56,610  -->  00:03:03,060
with a lower case r to make a raw string which we covered in the regular expressions lessons and you

42

00:03:03,060  -->  00:03:10,150
won't have and you won't have any escape characters there.

43

00:03:11,220  -->  00:03:16,800
So News I'm on Windows so using these backslashes is the proper separation for each folder.

44

00:03:17,040  -->  00:03:25,920
If I have a whole bunch of names for different folders like folder one folder two fold or three and

45

00:03:25,920  -->  00:03:28,110
then some kind of file.

46

00:03:28,110  -->  00:03:33,630
After that I could just use the join string method

47

00:03:38,100  -->  00:03:42,470
to create a file path string and have all these individual folder and file names.

48

00:03:42,690  -->  00:03:48,210
But this code would only work on Windows and ideally you want your python script to work on all operating

49

00:03:48,210  -->  00:03:49,330
systems.

50

00:03:49,530  -->  00:03:52,950
So Python gives you this module called OS.

51

00:03:53,030  -->  00:03:59,130
What's important right now in the OS module contains lots of different file path related functions that

52

00:03:59,130  -->  00:03:59,830
we can use.

53

00:03:59,880  -->  00:04:02,160
One of them is post-up path.

54

00:04:02,190  -->  00:04:10,140
Join misses a join function that is inside of a path module which is inside of the OS module.

55

00:04:10,200  -->  00:04:12,180
And this just takes several string arguments

56

00:04:15,770  -->  00:04:21,510
and it returns the string value of a path that's appropriate for the operating system that you're running

57

00:04:21,510  -->  00:04:21,900
this on.

58

00:04:21,900  -->  00:04:25,250
So I'm on Windows so it just uses the backslash character.

59

00:04:25,270  -->  00:04:31,110
If I was running this exact same code on Linux or Mac would be using the forward slash right here.

60

00:04:31,110  -->  00:04:38,230
In fact the value that the join function uses is stored in a set to variable inside the os module.

61

00:04:38,470  -->  00:04:42,950
Then just like every file has a file path that it is located in.

62

00:04:42,960  -->  00:04:48,830
Every program has a setting called the current working directory directory is just an older name for

63

00:04:48,840  -->  00:04:50,250
folder.

64

00:04:50,270  -->  00:04:55,170
The current working directory tells the program what folder it should look in when we just handed a

65

00:04:55,170  -->  00:04:57,220
file name without a file path.

66

00:04:57,560  -->  00:05:03,420
You can get the current working directory as a string value with the O S done get CW d the current working

67

00:05:03,420  -->  00:05:05,500
directory function.

68

00:05:05,580  -->  00:05:11,630
So no if I just give some sort of file name to some sort of file related function and it's just Stand-Up

69

00:05:11,640  -->  00:05:16,020
DNG without any sort of file path location information.

70

00:05:16,040  -->  00:05:17,170
By then I will just assume.

71

00:05:17,190  -->  00:05:18,050
Oh I guess.

72

00:05:18,120  -->  00:05:21,300
My current working directory is this folder right here.

73

00:05:21,360  -->  00:05:27,460
So I'm going to assume that spammed up here in G can be found inside their you also change the current

74

00:05:27,460  -->  00:05:32,310
working directory with the change directory function.

75

00:05:32,470  -->  00:05:35,080
And this is just past a new file path.

76

00:05:35,140  -->  00:05:39,390
So I could say Change this to the root fire root folder.

77

00:05:39,490  -->  00:05:44,050
And now when I call get TWD it will return this updated version.

78

00:05:44,050  -->  00:05:52,710
Now if I ever use spammed DNG in some sort of function like delete this file this isn't a real function

79

00:05:52,720  -->  00:05:59,440
I'm just coming up with a random name Python or soon that spammed up DNG can be found inside of the

80

00:05:59,440  -->  00:06:00,820
current working directory.

81

00:06:00,820  -->  00:06:08,620
This is the root folder currently so something like spand up Ningi is an example of a relative file

82

00:06:08,620  -->  00:06:09,290
path.

83

00:06:09,290  -->  00:06:11,760
The there are two kinds of file paths.

84

00:06:11,800  -->  00:06:16,090
There's an absolute file path which always begins with the root folder.

85

00:06:16,090  -->  00:06:19,800
It gives you the complete location of a program.

86

00:06:20,140  -->  00:06:24,210
This was in folder one slash folder to slash spand up hangi.

87

00:06:24,250  -->  00:06:30,730
This is an absolute file path while a relative file path is always going to be relative to the current

88

00:06:30,730  -->  00:06:31,650
working directory.

89

00:06:31,720  -->  00:06:37,570
It's not going to begin with the root folder but I could have something like spammed up DNG it's going

90

00:06:37,570  -->  00:06:43,310
to assume that it's split the spand up DNG file that's in particular in the root folder.

91

00:06:43,600  -->  00:06:50,230
No relative file path can also begin with other folders.

92

00:06:50,230  -->  00:06:57,070
In this case it's going to assume that this relative file path means it's inside of the root folder

93

00:06:57,220  -->  00:07:02,430
and inside that root folder is a folder called Folder 1 and inside there is folder 2 and then spammed

94

00:07:02,430  -->  00:07:06,440
up whingy inside that folder is the file that I'm talking about.

95

00:07:06,700  -->  00:07:12,640
So absolute file paths always begin with the root folder and relative file paths do not.

96

00:07:12,700  -->  00:07:16,420
Now there is also the dot and the DOT DOT folders.

97

00:07:16,420  -->  00:07:21,050
These aren't real folders but special names that can be used in a relative file path.

98

00:07:21,130  -->  00:07:29,110
The single dot stands for this directory or this folder whereas two dots means the parent folder.

99

00:07:29,890  -->  00:07:32,230
Let me show you an example.

100

00:07:33,250  -->  00:07:35,580
So let's say our harddrive looked like this.

101

00:07:35,620  -->  00:07:42,370
Here's the C-Drive root folder and in there I have a folder called bacon and a folder called eggs and

102

00:07:42,370  -->  00:07:48,130
a file called spam that takes t and meanwhile inside that Baycon folder is a folder called fills and

103

00:07:48,130  -->  00:07:50,980
inside that folder is a spammed RTX file.

104

00:07:50,980  -->  00:07:56,190
Note that this spam that DXi file is a different file from this one because they're in different locations

105

00:07:56,230  -->  00:07:58,060
even though they have the same name.

106

00:07:58,060  -->  00:08:01,210
Let's say that my program is currently.

107

00:08:01,780  -->  00:08:07,820
And let's say my program has its current working directory set to this folder see slash bacon.

108

00:08:07,970  -->  00:08:11,950
Now the absolute path for this is just going to be c slash bacon.

109

00:08:11,950  -->  00:08:16,450
In fact all of the absolute paths you can see here begin with the root folder.

110

00:08:16,550  -->  00:08:19,750
But let's say I wanted to refer to other folders or files.

111

00:08:19,750  -->  00:08:24,650
I could also use relative paths and those are paths that don't begin with a root folder.

112

00:08:24,670  -->  00:08:27,840
Instead they usually begin with this dot just meaning this folder.

113

00:08:28,010  -->  00:08:35,410
So if I'm currently in the C slash baken folder with the current working directory I could just use

114

00:08:35,410  -->  00:08:40,240
dot slash to refer to this folder itself if I want to refer to that folder.

115

00:08:40,240  -->  00:08:45,670
Let's say I want to refer to the files folder inside of bacon when Bacon is the current working directory

116

00:08:45,670  -->  00:08:45,700
.

117

00:08:45,700  -->  00:08:51,400
I could just say well in this folder which has begun there is another name fizz and if I want to refer

118

00:08:51,400  -->  00:08:56,620
to the spam file inside that folder I could say well this folder there is a folder called pizz and then

119

00:08:56,620  -->  00:08:58,540
a file called Spanta RTX.

120

00:08:58,600  -->  00:09:04,240
But if you want to refer to files and folders that were above the bacon folder like the root folder

121

00:09:04,390  -->  00:09:07,420
you would need that dot dot syntax meaning the parent folder.

122

00:09:07,600  -->  00:09:09,840
So current working directory is set to bake in.

123

00:09:09,940  -->  00:09:15,190
I went to work for to the C-Drive with a realtor file path instead of saying this folder.

124

00:09:15,190  -->  00:09:21,490
I would say this parent folder or if I wanted to refer to say this spammed at DXi file inside of egg's

125

00:09:21,490  -->  00:09:27,070
here I would have to say well OK so I'm in C slash baken for my current working directory.

126

00:09:27,070  -->  00:09:32,980
I want to go up to that parent folder to see slash and then inside there is going to be eggs and spammed

127

00:09:32,980  -->  00:09:34,420
RTX T.

128

00:09:34,770  -->  00:09:36,160
Now for relative file paths.

129

00:09:36,160  -->  00:09:41,050
This does slash at the beginning is optional you could either use dot slash just to formally say this

130

00:09:41,050  -->  00:09:46,840
folder or you could just begin it with is slash band at DXi as long as it doesn't begin with the root

131

00:09:46,840  -->  00:09:48,260
folder name.

132

00:09:49,110  -->  00:09:52,840
So let's check out some more functions in the Astar path module.

133

00:09:52,840  -->  00:09:59,680
I'm going to go ahead and change the current working directory back to what it was before.

134

00:09:59,950  -->  00:10:07,360
There's a function called OS path that Abz path and this will return a string of an absolute path of

135

00:10:07,360  -->  00:10:09,300
the path that you pass it.

136

00:10:09,310  -->  00:10:12,260
So if I pass it spammed up P and G.

137

00:10:12,300  -->  00:10:16,620
This is a relative file path because it doesn't begin with the root folder.

138

00:10:16,620  -->  00:10:19,330
This will say OK you passed me spand up g.

139

00:10:19,360  -->  00:10:22,140
My current working directory is this folder.

140

00:10:22,270  -->  00:10:27,990
So the absolute file path at your X asking for is going to be this complete path.

141

00:10:29,610  -->  00:10:37,410
Or if I wanted to say I'm talking about the parent folder's parent folder there is a span p and g file

142

00:10:37,640  -->  00:10:41,230
will then say OK the absolute file path that you're talking about is this.

143

00:10:41,250  -->  00:10:48,310
Nancy it went up from this folder and then up again from this folder and then it said ok and then bam

144

00:10:48,320  -->  00:10:50,850
got PMG inside of that folder.

145

00:10:50,850  -->  00:10:55,270
So this is an easy way to convert a relative file path into an absolute one.

146

00:10:55,740  -->  00:11:01,620
There's also a function called is Abz for is absolute path.

147

00:11:01,620  -->  00:11:06,780
And this lets you pass it any file path you want and will return true if this is an absolute path.

148

00:11:06,780  -->  00:11:11,890
It's basically a simple way to programmatically determine if something begins with a root folder.

149

00:11:12,060  -->  00:11:15,450
So this is a relative file path so it returns false.

150

00:11:15,690  -->  00:11:18,230
Whereas if I passed it.

151

00:11:18,780  -->  00:11:23,890
So you can slash folder slash folder.

152

00:11:24,480  -->  00:11:30,730
Since this begins with the root folder is absolute path returns true.

153

00:11:31,530  -->  00:11:37,610
And also there is a function called Redpath which will give you the relative path between two paths

154

00:11:37,640  -->  00:11:38,960
that you give it.

155

00:11:38,970  -->  00:11:51,450
So if I said well this path folder one such folder to span that PMG and let's pretend the current working

156

00:11:51,450  -->  00:11:59,160
directory was just a folder one relative path would give you a relative path from this starting path

157

00:12:00,360  -->  00:12:01,860
to this other path.

158

00:12:02,040  -->  00:12:08,130
You can see it's inside a folder too which is inside folder one in there would be spanned up hangi.

159

00:12:08,150  -->  00:12:10,870
So that's how you this is the relative path.

160

00:12:11,280  -->  00:12:15,950
Given this current working directory that we'd go to this path.

161

00:12:16,130  -->  00:12:20,780
So it gives you the relative file path from some starting point.

162

00:12:20,780  -->  00:12:26,500
Now let's say you had a file path that was something like this see folder one folder to slash spammed

163

00:12:26,500  -->  00:12:33,390
up TNG and you just wanted to pull out say just the directory part of this or just the file name part

164

00:12:33,390  -->  00:12:43,770
of this is a function called postop path durning which will retrieve the directory part of that.

165

00:12:43,830  -->  00:12:51,120
There's also a function called basename which will pull out the last part the part out of this string

166

00:12:51,150  -->  00:12:53,460
after the final set of slashes.

167

00:12:53,500  -->  00:12:59,340
And this will also work not just with file names but also pull off the last folder from that as well

168

00:12:59,350  -->  00:13:00,110
.

169

00:13:00,990  -->  00:13:03,450
So you can visualize what this is doing right here.

170

00:13:03,540  -->  00:13:10,470
So durning name pulls off this section of a file path whereas basename pulls off this section right

171

00:13:10,470  -->  00:13:16,100
after the final slash.

172

00:13:16,260  -->  00:13:20,970
So all of these functions have just been nice string manipulation functions so that you don't have to

173

00:13:20,970  -->  00:13:23,210
write that code yourself.

174

00:13:23,370  -->  00:13:25,860
You actually want to check the files on your hard drive.

175

00:13:25,860  -->  00:13:33,360
There's a function called estopped path that exists which you can pass it a file path and will return

176

00:13:33,360  -->  00:13:38,730
true if this file actually exists and false if it does not this is just some file path that I've made

177

00:13:38,730  -->  00:13:40,500
up so it's going to return false.

178

00:13:40,610  -->  00:13:48,920
I could also pass it slash windows slash system 32 Kelk which is Windows calculator program.

179

00:13:49,270  -->  00:13:50,160
Whoops.

180

00:13:50,160  -->  00:13:50,490
Forgot.

181

00:13:50,490  -->  00:13:58,890
It's existe plural not singular exists and it would say yes there is actually a file with this name

182

00:13:58,890  -->  00:13:59,790
and path.

183

00:13:59,790  -->  00:14:06,500
Furthermore you could see something like this is a folder name or a file name with the OS type path

184

00:14:06,510  -->  00:14:09,060
that is file function.

185

00:14:09,540  -->  00:14:14,570
Whereas if you just passed it the folder part that would return false.

186

00:14:14,610  -->  00:14:17,560
Since this is just a folder not a file.

187

00:14:17,610  -->  00:14:25,950
And conversely there is also a function that will tell you if a file path is for a directory that is

188

00:14:25,950  -->  00:14:28,280
a folder.

189

00:14:29,970  -->  00:14:34,660
Let's cover a couple more functions that let us examine the contents of our harddrive.

190

00:14:34,730  -->  00:14:41,700
Also the path that gets size which we can pass the path for a file and it will return the size in bytes

191

00:14:41,730  -->  00:14:43,540
as an integer.

192

00:14:43,560  -->  00:14:49,360
So if I wanted to see how big that calculator program is you can see it's almost a megabyte about 900000

193

00:14:49,420  -->  00:14:50,350
bytes.

194

00:14:50,760  -->  00:14:58,790
And the OS does Lister notice that this function is not inside the path module it's just inside OS.

195

00:14:59,070  -->  00:15:08,910
You can pass Lister the file path of a folder and it will then return a list of strings with the file

196

00:15:08,910  -->  00:15:13,270
names and folder names that are inside that folder that you've passed it.

197

00:15:13,830  -->  00:15:19,640
So inside my automate book folder I have all these files and folders.

198

00:15:19,930  -->  00:15:25,170
So let's combine these two functions into a small program that will retrieve the size of all the files

199

00:15:25,200  -->  00:15:26,720
in a certain folder.

200

00:15:26,730  -->  00:15:32,040
This would be pretty simple we can just have a variable called total size that starts off as 0 and then

201

00:15:32,040  -->  00:15:40,970
we'll just create a for loop that just iterates over the files returned by 0 estopped Lister.

202

00:15:41,010  -->  00:15:46,740
So each of those file names that are returned in this list and remember that this list will also contain

203

00:15:46,770  -->  00:15:48,390
folder names as well as file names.

204

00:15:48,390  -->  00:15:55,770
We only want to look at the file names so I could have some code that checks like if that path is file

205

00:15:57,050  -->  00:16:03,360
and then well it can't be file name because I have to provide the full path for it so I'll just provide

206

00:16:04,390  -->  00:16:05,910
a path that join

207

00:16:09,060  -->  00:16:13,050
automate book and then the file name.

208

00:16:13,110  -->  00:16:20,100
And so if this is not a file path song and I add the not boolean operator here that's not then whoops

209

00:16:20,160  -->  00:16:27,300
I had some syntax error here because I forgot to print the C and that print to see right there.

210

00:16:27,810  -->  00:16:30,840
So if that's not a file then I'll just continue.

211

00:16:31,170  -->  00:16:40,820
Otherwise I want to have total size be increased by the amount returned by post-up that gets size.

212

00:16:41,070  -->  00:16:43,970
And this will be for the absolute file name.

213

00:16:45,030  -->  00:16:51,360
So I'm just going to copy and paste that part and I'll just press enter and Python will run all of this

214

00:16:51,360  -->  00:16:59,390
code in this loop and total size will be set to the size of all the files inside this folder.

215

00:16:59,460  -->  00:17:04,130
So you can use code like this in a program of your own and less unless I'm just going to cover the OS

216

00:17:04,130  -->  00:17:06,980
to make yours function.

217

00:17:07,140  -->  00:17:13,110
So if you want to create new folders you can just pass this either a relative or an absolute file path

218

00:17:13,410  -->  00:17:16,650
and it will create all the folders that you specify.

219

00:17:16,650  -->  00:17:22,020
So if I said I want to create a new folder called Delicious in the root folder path but not just that

220

00:17:22,050  -->  00:17:27,390
I want to create that folder and a nother folder inside that folder called Walnut.

221

00:17:27,450  -->  00:17:33,990
Another folder inside that folder called waffles could run over Statten make der's and Python will then

222

00:17:33,990  -->  00:17:36,510
create all of these folders for you.

223

00:17:36,520  -->  00:17:44,280
I can open up the file the file explorer click on C-Drive and find that there is now a folder called

224

00:17:44,280  -->  00:17:44,920
Delicious.

225

00:17:44,940  -->  00:17:50,550
And inside there there is a folder called Walnut and there is a folder called waffles.

226

00:17:50,670  -->  00:17:54,600
We've covered a lot of different functions in the OS and OS type path modules.

227

00:17:54,600  -->  00:17:56,400
Don't worry about memorizing all of them.

228

00:17:56,400  -->  00:18:00,960
Just remember that they exist and you can look at the exact documentation for them later when you need

229

00:18:00,960  -->  00:18:01,750
it.

230

00:18:01,890  -->  00:18:05,190
To recap files have a name and a path.

231

00:18:05,280  -->  00:18:09,120
The root folder is the lowest folder that contains all of their folders.

232

00:18:09,120  -->  00:18:11,150
On Windows it's Seacole and slash.

233

00:18:11,190  -->  00:18:18,040
And on Linux and Mac it's just slash in a file path of folders and file names are separated by backslashes

234

00:18:18,060  -->  00:18:20,960
on Windows and forward slashes on Linux and Mac.

235

00:18:21,390  -->  00:18:26,520
Use the OS start path that join function to combine folders with the correct slash for that operating

236

00:18:26,520  -->  00:18:28,010
system.

237

00:18:28,020  -->  00:18:33,330
All programs have a current working directory setting which is a folder that any relative paths are

238

00:18:33,330  -->  00:18:39,990
relative to the OS stop get CW D function will return the current working directory and change or will

239

00:18:40,050  -->  00:18:43,050
change the current working directory absolute path.

240

00:18:43,050  -->  00:18:44,390
Begin with the root folder.

241

00:18:44,430  -->  00:18:46,680
Whereas relative paths do not.

242

00:18:46,830  -->  00:18:52,900
The doc folder represents this folder and the DOT DOT folder represents the parent folder.

243

00:18:53,040  -->  00:18:57,890
The absolute path function will return the absolute path form of any path that you pass to.

244

00:18:58,410  -->  00:19:03,810
Whereas the is absolute function will return true if that is the path that you passed to it is an absolute

245

00:19:03,810  -->  00:19:04,820
path and false.

246

00:19:04,830  -->  00:19:10,830
If it's a relative path real path returns the relative path between the two paths that you pass to it

247

00:19:10,830  -->  00:19:11,520
.

248

00:19:11,550  -->  00:19:13,480
Make Dors can make folders for you.

249

00:19:13,500  -->  00:19:17,670
Whereas estopped pathic get size will return a file size.

250

00:19:17,740  -->  00:19:24,630
Lister will return a list of strings of file and folder names exist for return true if a file or folder

251

00:19:24,630  -->  00:19:30,870
that you pass to it exist and is file and is dürer will return true if they were past a file or directory

252

00:19:30,870  -->  00:19:31,890
name.
