1
00:00:00,690 --> 00:00:01,020
All right.

2
00:00:01,020 --> 00:00:06,020
So we've seen how we can open and close files as well as read them and write to

3
00:00:07,680 --> 00:00:11,670
them. But files don't just have a name.

4
00:00:11,700 --> 00:00:16,140
They also have a path or a way of getting to them.

5
00:00:16,680 --> 00:00:18,870
For example, here in this map,

6
00:00:19,110 --> 00:00:21,990
if we wanted this dinosaur to get to the golden egg,

7
00:00:22,320 --> 00:00:24,690
there's many paths that it could take.

8
00:00:24,900 --> 00:00:29,900
But if we wanted to give it some directions as to how it might arrive on this

9
00:00:30,180 --> 00:00:35,100
path, well we might say, you know, continue forwards at the volcano turn, right.

10
00:00:35,490 --> 00:00:38,340
And then when you see a snake turn right again,

11
00:00:38,700 --> 00:00:42,900
and when you see the coconut tree turn left and you will get to the cave with

12
00:00:42,900 --> 00:00:47,100
the egg. Now on computers, it works quite similarly,

13
00:00:47,400 --> 00:00:52,380
but we have to remember that on the computer there's files and there's folders.

14
00:00:52,890 --> 00:00:57,890
So files can live within folders and you can navigate through several folders

15
00:00:58,050 --> 00:01:00,420
deep in order to get to a particular file.

16
00:01:01,260 --> 00:01:06,260
So let's think about what the file paths might be for a computer that has a

17
00:01:08,160 --> 00:01:09,570
structure that looks like this.

18
00:01:10,020 --> 00:01:15,020
So the first thing to note is that at the very root of all of the files and the

19
00:01:16,170 --> 00:01:20,940
folders is basically the origin, right? This is the root folder.

20
00:01:21,420 --> 00:01:22,800
And on a Mac,

21
00:01:22,830 --> 00:01:27,830
you can see that our PyCharm projects live within my users folder, inside a

22
00:01:30,120 --> 00:01:33,990
folder called users inside a folder called Macintosh HD.

23
00:01:34,410 --> 00:01:38,250
So if I have this folder selected right here, I can say

24
00:01:38,280 --> 00:01:41,880
go to enclosing folder, which is this one.

25
00:01:42,270 --> 00:01:43,860
And then if I keep doing this,

26
00:01:43,860 --> 00:01:48,270
you can see it keeps jumping up until it can go no further.

27
00:01:48,810 --> 00:01:53,790
So the root on a Mac is the Macintosh HD, the hard drive.

28
00:01:54,330 --> 00:01:59,330
And that in terms of a path is represented by a single forward slash.

29
00:02:01,500 --> 00:02:04,260
Now when you're on a Windows computer however,

30
00:02:04,860 --> 00:02:08,759
the root folder is usually your C drive.

31
00:02:09,449 --> 00:02:14,160
So that becomes your root folder. Inside our root folder, our

32
00:02:14,440 --> 00:02:16,950
Macintosh HD or a C drive,

33
00:02:17,190 --> 00:02:20,670
we have a folder called work. Inside that folder

34
00:02:20,790 --> 00:02:25,650
I've got a report Word document as well as a project folder.

35
00:02:25,710 --> 00:02:30,120
And then inside that project folder, there's a PowerPoint called talk.

36
00:02:30,660 --> 00:02:34,380
Now, if we think about the file paths for each of these things,

37
00:02:34,650 --> 00:02:36,450
this is what they might look like.

38
00:02:36,510 --> 00:02:41,510
The route is obviously just a /, the work folder will be 

39
00:02:41,700 --> 00:02:46,700
/work, report will be /work/ report.doc, project will

40
00:02:48,240 --> 00:02:51,840
be /work/project, and finally,

41
00:02:51,840 --> 00:02:56,840
the talk will be /work/project/talk.

42
00:02:57,000 --> 00:02:57,833
ppt.

43
00:02:58,140 --> 00:03:02,230
So this path is kind of like the direction that we were talking about

44
00:03:02,230 --> 00:03:03,063
previously.

45
00:03:03,370 --> 00:03:07,690
It's a way for the computer to navigate to the file of interest

46
00:03:07,990 --> 00:03:10,930
which in this case might be the talk.ppt.

47
00:03:11,590 --> 00:03:15,790
And we have to give it directions for which folder to go into and then which

48
00:03:15,790 --> 00:03:20,440
folder, and then you'll find the file with this name. Now,

49
00:03:20,470 --> 00:03:23,740
this is known as an absolute file path.

50
00:03:23,830 --> 00:03:28,830
Absolute file paths always start off relative to the root.

51
00:03:29,290 --> 00:03:32,590
So you always see a forward slash to begin with, and sometimes you'll see

52
00:03:32,590 --> 00:03:34,720
C: or on a Mac

53
00:03:34,720 --> 00:03:38,680
you'll just see the /. This absolute file

54
00:03:38,680 --> 00:03:42,010
path is basically a path that starts from the origin,

55
00:03:42,040 --> 00:03:44,680
the route of the computer storage system.

56
00:03:45,370 --> 00:03:49,630
Now there's also something called a relative file path.

57
00:03:49,960 --> 00:03:52,180
Taking this talk.ppt,

58
00:03:52,270 --> 00:03:57,270
we know that the absolute file path is /work/project/

59
00:03:57,640 --> 00:03:59,860
talk.ppt. But

60
00:04:00,000 --> 00:04:04,980
what if we were actually inside this project folder?

61
00:04:05,460 --> 00:04:10,140
So let's say that at the time when we're trying to get to this talk.ppt

62
00:04:10,440 --> 00:04:12,450
we're actually right here.

63
00:04:13,350 --> 00:04:18,089
Now this, in computer-speak, is called the working directory.

64
00:04:18,450 --> 00:04:23,100
It's basically the directory or folder that we're currently working from.

65
00:04:23,700 --> 00:04:27,210
And once we've established a working directory,

66
00:04:27,390 --> 00:04:29,310
say that we are right here,

67
00:04:29,670 --> 00:04:34,380
then we can use what's called a relative file path to get to a file that we're

68
00:04:34,380 --> 00:04:38,430
interested in. So given that we're here and we're trying to get here,

69
00:04:38,790 --> 00:04:43,790
the relative file path is simply just ./talk.ppt.

70
00:04:45,780 --> 00:04:47,730
The ./ signifies

71
00:04:47,970 --> 00:04:51,780
look in the current folder for this file.

72
00:04:52,350 --> 00:04:57,350
If we switch up our working directory and let's say we are now working from the

73
00:04:58,350 --> 00:05:02,820
work folder, how do we get to this talk.ppt? Well,

74
00:05:02,850 --> 00:05:05,820
the absolute file path would not change for this file,

75
00:05:06,150 --> 00:05:11,150
but the relative file path would now be ./project/talk.ppt.

76
00:05:13,530 --> 00:05:17,310
So we're walking from here to here to here.

77
00:05:18,030 --> 00:05:22,260
Now, what if we wanted to go upwards in the directory tree?

78
00:05:22,620 --> 00:05:26,220
Let's say we wanted to get a hold of this report.doc,

79
00:05:26,520 --> 00:05:31,200
but we all currently working inside this project folder.

80
00:05:31,500 --> 00:05:36,360
How do we go one step up? Well, we would right ..,

81
00:05:36,420 --> 00:05:40,620
so two dots now, /report.doc.

82
00:05:40,860 --> 00:05:45,600
So we're inside this project folder, we're coming out of it to the parent folder

83
00:05:45,600 --> 00:05:49,800
which is the work folder, and then we're getting hold of this file.

84
00:05:50,130 --> 00:05:55,130
So that two dots represents going one step up in the hierarchy to the parent

85
00:05:56,160 --> 00:05:58,700
folder, which this case is the work folder.

86
00:05:59,480 --> 00:06:04,480
Now what we've been doing is we've been writing code inside our main.py.

87
00:06:05,360 --> 00:06:10,250
And that main.py has usually been inside some sort of project folder. When

88
00:06:10,250 --> 00:06:14,780
we're trying to get hold of a file this is basically where we are at.

89
00:06:14,810 --> 00:06:18,260
So our working directory is the work folder.

90
00:06:18,980 --> 00:06:23,980
So that means if we wanted to get access to this report.doc file from our

91
00:06:24,590 --> 00:06:25,580
main.py,

92
00:06:25,910 --> 00:06:30,910
all we need to do is write ./report.doc because we're working within

93
00:06:32,240 --> 00:06:36,230
the same directory and we're just getting hold of this file.

94
00:06:36,860 --> 00:06:41,510
Now, when you're using relative file path, you can use this ./

95
00:06:41,750 --> 00:06:44,870
but you can also shorten it and get rid of it as well.

96
00:06:45,560 --> 00:06:50,560
This is why we've been able to open this my_file.txt

97
00:06:50,930 --> 00:06:55,930
which is in the same folder as my main.py and notice how these two files are at

98
00:06:57,710 --> 00:07:02,060
the same hierarchical level. So let's do a quick challenge.

99
00:07:02,090 --> 00:07:06,050
I'm going to delete the part where we write to the file and I'm going to change

100
00:07:06,050 --> 00:07:06,560
my file

101
00:07:06,560 --> 00:07:11,560
so it just has one sentence and I'm going to locate this file by using 'Reveal in

102
00:07:14,120 --> 00:07:17,990
Finder' and on Windows that would be right-

103
00:07:17,990 --> 00:07:20,390
click and Show in Explorer.

104
00:07:21,350 --> 00:07:26,350
And it would show you the location of that file in your computer system.

105
00:07:27,740 --> 00:07:32,090
Now, what we're going to do is we're going to move this file to the desktop.

106
00:07:32,380 --> 00:07:33,213
Right?

107
00:07:37,330 --> 00:07:41,320
And what I want you to do is I want you to go to the desktop, right-

108
00:07:41,320 --> 00:07:45,820
click on that file. And if you want a Mac, I want you to click on get info.

109
00:07:46,360 --> 00:07:47,710
And if you are on a Windows,

110
00:07:47,740 --> 00:07:51,400
I want you to right-click and go to properties.

111
00:07:52,000 --> 00:07:55,510
Now what you're looking for is this location here.

112
00:07:56,350 --> 00:07:58,930
Now you can see that this current file,

113
00:07:58,960 --> 00:08:03,340
new_file, is located under the root folder,

114
00:08:03,370 --> 00:08:08,370
which in this case is the C:\Users\London

115
00:08:09,220 --> 00:08:14,220
AppBrewery\Desktop\new_file.txt. On a Mac

116
00:08:15,640 --> 00:08:18,880
you can see that it's located under the root folder,

117
00:08:18,910 --> 00:08:23,910
which is Macintosh HD slash users slash Angela slash desktop slash my_file.

118
00:08:26,290 --> 00:08:29,470
txt. Here comes your challenge.

119
00:08:29,590 --> 00:08:33,010
Now that we've moved our file away from our project,

120
00:08:33,490 --> 00:08:37,120
I want you to fix this code so that when you run it,

121
00:08:37,299 --> 00:08:42,299
it won't give you this file not found error because at the moment there is no my_

122
00:08:43,419 --> 00:08:48,220
file.txt in the same working directory as our main.py.

123
00:08:48,280 --> 00:08:50,350
It doesn't exist inside this folder.

124
00:08:51,010 --> 00:08:55,620
Have a think about what you've learned so far and see if you can change this

125
00:08:55,620 --> 00:08:58,740
path so that our code works again.

126
00:08:59,310 --> 00:09:00,990
And it's the same on Windows.

127
00:09:01,380 --> 00:09:05,760
Use the location of the file on your computer and use the absolute path in the

128
00:09:05,760 --> 00:09:08,430
code to get it to work. Pause the video now.

129
00:09:08,630 --> 00:09:09,463
Okay

130
00:09:12,020 --> 00:09:16,250
All right. Here's how you would use the absolute path. So we know that our,

131
00:09:16,280 --> 00:09:21,280
my_file.txt is located in the root folder and then it's

132
00:09:21,470 --> 00:09:25,670
users, angela, desktop. So let's go ahead and change this string.

133
00:09:25,700 --> 00:09:29,750
Let's start at the root and then let's add the next folder,

134
00:09:29,780 --> 00:09:31,640
which is users.

135
00:09:32,180 --> 00:09:36,200
And then let's add another slash and then we go to the next folder,

136
00:09:36,230 --> 00:09:41,090
which is Angela. Now of course, this will be different for you,

137
00:09:41,630 --> 00:09:46,630
but finally we go to our desktop and then we can get to our,

138
00:09:48,320 --> 00:09:49,670
my_file.txt.

139
00:09:49,940 --> 00:09:53,420
So this is the full path to get to this file.

140
00:09:53,930 --> 00:09:58,370
So now if I run this code again, you can see it's able to read the file,

141
00:09:58,370 --> 00:10:02,090
there is no errors, and it's getting hold of the contents of the file.

142
00:10:02,810 --> 00:10:05,480
Now on Windows, it's a similar story.

143
00:10:05,720 --> 00:10:08,930
So we know that the location of the file is root,

144
00:10:09,320 --> 00:10:12,650
then users, then LondonAppBrewery, then desktop. Again,

145
00:10:12,710 --> 00:10:15,980
this of course will be different for you depending on your username,

146
00:10:16,280 --> 00:10:18,260
but you should see it in your location

147
00:10:18,290 --> 00:10:22,100
when you take a look at the file properties. So let's change this.

148
00:10:22,280 --> 00:10:25,040
Let's start out at the root, which is the C drive,

149
00:10:25,550 --> 00:10:27,770
and then let's go to users.

150
00:10:30,590 --> 00:10:34,670
And then we go to our username which is LondonAppBrewery

151
00:10:34,880 --> 00:10:35,713
,

152
00:10:38,690 --> 00:10:43,690
and then we go to our desktop and that is where it should find this new_file.

153
00:10:45,410 --> 00:10:48,140
txt. So now when I hit run,

154
00:10:48,200 --> 00:10:53,200
you can see that it's able to fetch that file and print the content without any

155
00:10:54,170 --> 00:10:55,760
errors. Now,

156
00:10:55,760 --> 00:11:00,760
one of the peculiarities about file paths in Windows and Mac is that in a Mac,

157
00:11:02,780 --> 00:11:03,740
the path,

158
00:11:03,890 --> 00:11:08,390
each of the folders are separated by a / whereas on Windows,

159
00:11:08,420 --> 00:11:12,710
they're separated by a \. But as you've noticed,

160
00:11:12,770 --> 00:11:16,070
when we're writing Python code, we don't care about that.

161
00:11:16,100 --> 00:11:21,100
We can just write it as / and making sure that we have the root,

162
00:11:21,350 --> 00:11:25,010
which represents the C drive, so this entire part,

163
00:11:25,430 --> 00:11:29,150
and then we have our folder, the next folder, the next folder,

164
00:11:29,150 --> 00:11:33,650
the next folder, et cetera. So now comes the next challenge.

165
00:11:34,110 --> 00:11:38,570
You can see that this path follows from the root folder,

166
00:11:38,570 --> 00:11:41,810
which is the C drive and on the Mac,

167
00:11:42,050 --> 00:11:44,420
it's also from the Macintosh HD.

168
00:11:44,840 --> 00:11:49,840
So what if we wanted to change this so that it's relative to our current main

169
00:11:50,570 --> 00:11:52,790
.py? Our main.py

170
00:11:53,350 --> 00:11:55,960
if we go to 'Show in Explorer',

171
00:11:56,290 --> 00:12:01,030
you can see that the path for this file is the following.

172
00:12:01,120 --> 00:12:01,720
It's under

173
00:12:01,720 --> 00:12:06,720
Users/LondonAppBrewery/PyCharmProjects/WindowsDemo.

174
00:12:07,780 --> 00:12:12,280
So that's the name of my project. And then afterwards,

175
00:12:12,340 --> 00:12:17,050
it's my main.py. So this is a bit of a tricky question.

176
00:12:17,470 --> 00:12:22,470
Given that this file is inside this current folder or whatever it is that you

177
00:12:23,170 --> 00:12:26,290
have as your folder which contains this file,

178
00:12:26,890 --> 00:12:31,890
how can you get it to use a relative file path so that it jumps back two folders

179
00:12:33,790 --> 00:12:38,790
to the LondonAppBrewery folder and then goes to Desktop and then to new file?

180
00:12:40,150 --> 00:12:44,590
On our Mac, it's a similar story. So we go to 'Reveal in Finder'.

181
00:12:44,620 --> 00:12:47,260
We can see this is where we are currently.

182
00:12:47,530 --> 00:12:52,530
So our current working directory is this day-24 project folder.

183
00:12:53,080 --> 00:12:57,040
So if we go one level up and the shortcut is command + up,

184
00:12:57,370 --> 00:13:02,260
we go to our day-24 folder. So this is our working directory.

185
00:13:02,740 --> 00:13:04,810
Now, if we want to go up one level,

186
00:13:04,810 --> 00:13:08,620
we can go to go, and then go to including folder,

187
00:13:08,860 --> 00:13:11,800
so one level up that gets us to PyCharm projects.

188
00:13:12,160 --> 00:13:16,690
And then if we go one level up further, we get to our Angela folder

189
00:13:17,080 --> 00:13:21,760
at which point we can then navigate downwards to Desktop/my_file.txt.

190
00:13:22,450 --> 00:13:23,770
This is your challenge.

191
00:13:23,980 --> 00:13:28,120
Figure out what the relative path to the text file on your desktop should be

192
00:13:28,390 --> 00:13:31,600
then modify the code and get it to work. Pause the video now.

193
00:13:34,600 --> 00:13:39,460
The way that we would modify this is in order to get to this Angela folder where

194
00:13:39,460 --> 00:13:42,940
we can continue to our Desktop and then to my file,

195
00:13:43,240 --> 00:13:45,130
we have to go up two levels.

196
00:13:45,190 --> 00:13:48,850
So that translates to ../../

197
00:13:48,940 --> 00:13:50,770
and now we're inside the Angela folder.

198
00:13:50,950 --> 00:13:55,750
So we can start going down. And PyCharm is smart enough to know what your

199
00:13:55,780 --> 00:13:57,220
folder structure looks like.

200
00:13:57,520 --> 00:14:01,960
So it will start giving you hints as to what folders are accessible at this

201
00:14:01,960 --> 00:14:02,793
level.

202
00:14:02,890 --> 00:14:07,420
So I can get to Desktop and then I can get to my_file.txt.

203
00:14:07,840 --> 00:14:10,060
And if I go ahead and run my code,

204
00:14:10,150 --> 00:14:15,070
you can see it's able to read and print the content of that file.

205
00:14:15,940 --> 00:14:20,650
The main difference between an absolute file path and a relative file path is

206
00:14:20,650 --> 00:14:25,650
the absolute file path is always relative to the root of your computer. On

207
00:14:26,710 --> 00:14:29,740
Windows that's the C drive, usually anyways

208
00:14:29,740 --> 00:14:32,290
unless you've changed it. And on a Mac,

209
00:14:32,380 --> 00:14:34,780
it is the Macintosh HD.

210
00:14:35,620 --> 00:14:40,210
Now the relative file path is relative to your current working directory.

211
00:14:40,210 --> 00:14:44,380
So it depends on where you are and where you're trying to get to. Now,

212
00:14:44,410 --> 00:14:49,360
depending on the situation and where that file you are interested in is located,

213
00:14:49,630 --> 00:14:53,870
you might decide to use the absolute file path or the relative file path.

214
00:14:54,550 --> 00:14:56,200
And you can see that in this case

215
00:14:56,200 --> 00:15:01,200
it's not necessarily that much shorter or that much more easier to understand.

216
00:15:01,840 --> 00:15:04,090
So if you know how to do both,

217
00:15:04,480 --> 00:15:08,050
then you can adapt to the situation and figure out which one is most

218
00:15:08,050 --> 00:15:12,130
appropriate. Now I know that paths can be confusing.

219
00:15:12,430 --> 00:15:16,330
So that's why I've come up with a whole bunch of quiz exercises for you in the

220
00:15:16,330 --> 00:15:20,530
next lesson for you to get more the practice with file paths so that you can

221
00:15:20,530 --> 00:15:22,540
feel confident leaving this lesson.

222
00:15:23,110 --> 00:15:26,140
So for all of that and more, I'll see you on the next lesson.

