WEBVTT

0
00:00.690 --> 00:01.020
All right.

1
00:01.020 --> 00:06.020
So we've seen how we can open and close files as well as read them and write to

2
00:07.680 --> 00:11.670
them. But files don't just have a name.

3
00:11.700 --> 00:16.140
They also have a path or a way of getting to them.

4
00:16.680 --> 00:18.870
For example, here in this map,

5
00:19.110 --> 00:21.990
if we wanted this dinosaur to get to the golden egg,

6
00:22.320 --> 00:24.690
there's many paths that it could take.

7
00:24.900 --> 00:29.900
But if we wanted to give it some directions as to how it might arrive on this

8
00:30.180 --> 00:35.100
path, well we might say, you know, continue forwards at the volcano turn, right.

9
00:35.490 --> 00:38.340
And then when you see a snake turn right again,

10
00:38.700 --> 00:42.900
and when you see the coconut tree turn left and you will get to the cave with

11
00:42.900 --> 00:47.100
the egg. Now on computers, it works quite similarly,

12
00:47.400 --> 00:52.380
but we have to remember that on the computer there's files and there's folders.

13
00:52.890 --> 00:57.890
So files can live within folders and you can navigate through several folders

14
00:58.050 --> 01:00.420
deep in order to get to a particular file.

15
01:01.260 --> 01:06.260
So let's think about what the file paths might be for a computer that has a

16
01:08.160 --> 01:09.570
structure that looks like this.

17
01:10.020 --> 01:15.020
So the first thing to note is that at the very root of all of the files and the

18
01:16.170 --> 01:20.940
folders is basically the origin, right? This is the root folder.

19
01:21.420 --> 01:22.800
And on a Mac,

20
01:22.830 --> 01:27.830
you can see that our PyCharm projects live within my users folder, inside a

21
01:30.120 --> 01:33.990
folder called users inside a folder called Macintosh HD.

22
01:34.410 --> 01:38.250
So if I have this folder selected right here, I can say

23
01:38.280 --> 01:41.880
go to enclosing folder, which is this one.

24
01:42.270 --> 01:43.860
And then if I keep doing this,

25
01:43.860 --> 01:48.270
you can see it keeps jumping up until it can go no further.

26
01:48.810 --> 01:53.790
So the root on a Mac is the Macintosh HD, the hard drive.

27
01:54.330 --> 01:59.330
And that in terms of a path is represented by a single forward slash.

28
02:01.500 --> 02:04.260
Now when you're on a Windows computer however,

29
02:04.860 --> 02:08.760
the root folder is usually your C drive.

30
02:09.450 --> 02:14.160
So that becomes your root folder. Inside our root folder, our

31
02:14.440 --> 02:16.950
Macintosh HD or a C drive,

32
02:17.190 --> 02:20.670
we have a folder called work. Inside that folder

33
02:20.790 --> 02:25.650
I've got a report Word document as well as a project folder.

34
02:25.710 --> 02:30.120
And then inside that project folder, there's a PowerPoint called talk.

35
02:30.660 --> 02:34.380
Now, if we think about the file paths for each of these things,

36
02:34.650 --> 02:36.450
this is what they might look like.

37
02:36.510 --> 02:41.510
The route is obviously just a /, the work folder will be 

38
02:41.700 --> 02:46.700
/work, report will be /work/ report.doc, project will

39
02:48.240 --> 02:51.840
be /work/project, and finally,

40
02:51.840 --> 02:56.840
the talk will be /work/project/talk.

41
02:57.000 --> 02:57.833
ppt.

42
02:58.140 --> 03:02.230
So this path is kind of like the direction that we were talking about

43
03:02.230 --> 03:03.063
previously.

44
03:03.370 --> 03:07.690
It's a way for the computer to navigate to the file of interest

45
03:07.990 --> 03:10.930
which in this case might be the talk.ppt.

46
03:11.590 --> 03:15.790
And we have to give it directions for which folder to go into and then which

47
03:15.790 --> 03:20.440
folder, and then you'll find the file with this name. Now,

48
03:20.470 --> 03:23.740
this is known as an absolute file path.

49
03:23.830 --> 03:28.830
Absolute file paths always start off relative to the root.

50
03:29.290 --> 03:32.590
So you always see a forward slash to begin with, and sometimes you'll see

51
03:32.590 --> 03:34.720
C: or on a Mac

52
03:34.720 --> 03:38.680
you'll just see the /. This absolute file

53
03:38.680 --> 03:42.010
path is basically a path that starts from the origin,

54
03:42.040 --> 03:44.680
the route of the computer storage system.

55
03:45.370 --> 03:49.630
Now there's also something called a relative file path.

56
03:49.960 --> 03:52.180
Taking this talk.ppt,

57
03:52.270 --> 03:57.270
we know that the absolute file path is /work/project/

58
03:57.640 --> 03:59.860
talk.ppt. But

59
04:00.000 --> 04:04.980
what if we were actually inside this project folder?

60
04:05.460 --> 04:10.140
So let's say that at the time when we're trying to get to this talk.ppt

61
04:10.440 --> 04:12.450
we're actually right here.

62
04:13.350 --> 04:18.090
Now this, in computer-speak, is called the working directory.

63
04:18.450 --> 04:23.100
It's basically the directory or folder that we're currently working from.

64
04:23.700 --> 04:27.210
And once we've established a working directory,

65
04:27.390 --> 04:29.310
say that we are right here,

66
04:29.670 --> 04:34.380
then we can use what's called a relative file path to get to a file that we're

67
04:34.380 --> 04:38.430
interested in. So given that we're here and we're trying to get here,

68
04:38.790 --> 04:43.790
the relative file path is simply just ./talk.ppt.

69
04:45.780 --> 04:47.730
The ./ signifies

70
04:47.970 --> 04:51.780
look in the current folder for this file.

71
04:52.350 --> 04:57.350
If we switch up our working directory and let's say we are now working from the

72
04:58.350 --> 05:02.820
work folder, how do we get to this talk.ppt? Well,

73
05:02.850 --> 05:05.820
the absolute file path would not change for this file,

74
05:06.150 --> 05:11.150
but the relative file path would now be ./project/talk.ppt.

75
05:13.530 --> 05:17.310
So we're walking from here to here to here.

76
05:18.030 --> 05:22.260
Now, what if we wanted to go upwards in the directory tree?

77
05:22.620 --> 05:26.220
Let's say we wanted to get a hold of this report.doc,

78
05:26.520 --> 05:31.200
but we all currently working inside this project folder.

79
05:31.500 --> 05:36.360
How do we go one step up? Well, we would right ..,

80
05:36.420 --> 05:40.620
so two dots now, /report.doc.

81
05:40.860 --> 05:45.600
So we're inside this project folder, we're coming out of it to the parent folder

82
05:45.600 --> 05:49.800
which is the work folder, and then we're getting hold of this file.

83
05:50.130 --> 05:55.130
So that two dots represents going one step up in the hierarchy to the parent

84
05:56.160 --> 05:58.700
folder, which this case is the work folder.

85
05:59.480 --> 06:04.480
Now what we've been doing is we've been writing code inside our main.py.

86
06:05.360 --> 06:10.250
And that main.py has usually been inside some sort of project folder. When

87
06:10.250 --> 06:14.780
we're trying to get hold of a file this is basically where we are at.

88
06:14.810 --> 06:18.260
So our working directory is the work folder.

89
06:18.980 --> 06:23.980
So that means if we wanted to get access to this report.doc file from our

90
06:24.590 --> 06:25.580
main.py,

91
06:25.910 --> 06:30.910
all we need to do is write ./report.doc because we're working within

92
06:32.240 --> 06:36.230
the same directory and we're just getting hold of this file.

93
06:36.860 --> 06:41.510
Now, when you're using relative file path, you can use this ./

94
06:41.750 --> 06:44.870
but you can also shorten it and get rid of it as well.

95
06:45.560 --> 06:50.560
This is why we've been able to open this my_file.txt

96
06:50.930 --> 06:55.930
which is in the same folder as my main.py and notice how these two files are at

97
06:57.710 --> 07:02.060
the same hierarchical level. So let's do a quick challenge.

98
07:02.090 --> 07:06.050
I'm going to delete the part where we write to the file and I'm going to change

99
07:06.050 --> 07:06.560
my file

100
07:06.560 --> 07:11.560
so it just has one sentence and I'm going to locate this file by using 'Reveal in

101
07:14.120 --> 07:17.990
Finder' and on Windows that would be right-

102
07:17.990 --> 07:20.390
click and Show in Explorer.

103
07:21.350 --> 07:26.350
And it would show you the location of that file in your computer system.

104
07:27.740 --> 07:32.090
Now, what we're going to do is we're going to move this file to the desktop.

105
07:32.380 --> 07:33.213
<v 1>Right?</v>

106
07:37.330 --> 07:41.320
<v 0>And what I want you to do is I want you to go to the desktop, right-</v>

107
07:41.320 --> 07:45.820
click on that file. And if you want a Mac, I want you to click on get info.

108
07:46.360 --> 07:47.710
And if you are on a Windows,

109
07:47.740 --> 07:51.400
I want you to right-click and go to properties.

110
07:52.000 --> 07:55.510
Now what you're looking for is this location here.

111
07:56.350 --> 07:58.930
Now you can see that this current file,

112
07:58.960 --> 08:03.340
new_file, is located under the root folder,

113
08:03.370 --> 08:08.370
which in this case is the C:\Users\London

114
08:09.220 --> 08:14.220
AppBrewery\Desktop\new_file.txt. On a Mac

115
08:15.640 --> 08:18.880
you can see that it's located under the root folder,

116
08:18.910 --> 08:23.910
which is Macintosh HD slash users slash Angela slash desktop slash my_file.

117
08:26.290 --> 08:29.470
txt. Here comes your challenge.

118
08:29.590 --> 08:33.010
Now that we've moved our file away from our project,

119
08:33.490 --> 08:37.120
I want you to fix this code so that when you run it,

120
08:37.300 --> 08:42.300
it won't give you this file not found error because at the moment there is no my_

121
08:43.420 --> 08:48.220
file.txt in the same working directory as our main.py.

122
08:48.280 --> 08:50.350
It doesn't exist inside this folder.

123
08:51.010 --> 08:55.620
Have a think about what you've learned so far and see if you can change this

124
08:55.620 --> 08:58.740
path so that our code works again.

125
08:59.310 --> 09:00.990
And it's the same on Windows.

126
09:01.380 --> 09:05.760
Use the location of the file on your computer and use the absolute path in the

127
09:05.760 --> 09:08.430
code to get it to work. Pause the video now.

128
09:08.630 --> 09:09.463
<v 1>Okay</v>

129
09:12.020 --> 09:16.250
<v 0>All right. Here's how you would use the absolute path. So we know that our,</v>

130
09:16.280 --> 09:21.280
my_file.txt is located in the root folder and then it's

131
09:21.470 --> 09:25.670
users, angela, desktop. So let's go ahead and change this string.

132
09:25.700 --> 09:29.750
Let's start at the root and then let's add the next folder,

133
09:29.780 --> 09:31.640
which is users.

134
09:32.180 --> 09:36.200
And then let's add another slash and then we go to the next folder,

135
09:36.230 --> 09:41.090
which is Angela. Now of course, this will be different for you,

136
09:41.630 --> 09:46.630
but finally we go to our desktop and then we can get to our,

137
09:48.320 --> 09:49.670
my_file.txt.

138
09:49.940 --> 09:53.420
So this is the full path to get to this file.

139
09:53.930 --> 09:58.370
So now if I run this code again, you can see it's able to read the file,

140
09:58.370 --> 10:02.090
there is no errors, and it's getting hold of the contents of the file.

141
10:02.810 --> 10:05.480
Now on Windows, it's a similar story.

142
10:05.720 --> 10:08.930
So we know that the location of the file is root,

143
10:09.320 --> 10:12.650
then users, then LondonAppBrewery, then desktop. Again,

144
10:12.710 --> 10:15.980
this of course will be different for you depending on your username,

145
10:16.280 --> 10:18.260
but you should see it in your location

146
10:18.290 --> 10:22.100
when you take a look at the file properties. So let's change this.

147
10:22.280 --> 10:25.040
Let's start out at the root, which is the C drive,

148
10:25.550 --> 10:27.770
and then let's go to users.

149
10:30.590 --> 10:34.670
And then we go to our username which is LondonAppBrewery

150
10:34.880 --> 10:35.713
,

151
10:38.690 --> 10:43.690
and then we go to our desktop and that is where it should find this new_file.

152
10:45.410 --> 10:48.140
txt. So now when I hit run,

153
10:48.200 --> 10:53.200
you can see that it's able to fetch that file and print the content without any

154
10:54.170 --> 10:55.760
errors. Now,

155
10:55.760 --> 11:00.760
one of the peculiarities about file paths in Windows and Mac is that in a Mac,

156
11:02.780 --> 11:03.740
the path,

157
11:03.890 --> 11:08.390
each of the folders are separated by a / whereas on Windows,

158
11:08.420 --> 11:12.710
they're separated by a \. But as you've noticed,

159
11:12.770 --> 11:16.070
when we're writing Python code, we don't care about that.

160
11:16.100 --> 11:21.100
We can just write it as / and making sure that we have the root,

161
11:21.350 --> 11:25.010
which represents the C drive, so this entire part,

162
11:25.430 --> 11:29.150
and then we have our folder, the next folder, the next folder,

163
11:29.150 --> 11:33.650
the next folder, et cetera. So now comes the next challenge.

164
11:34.110 --> 11:38.570
You can see that this path follows from the root folder,

165
11:38.570 --> 11:41.810
which is the C drive and on the Mac,

166
11:42.050 --> 11:44.420
it's also from the Macintosh HD.

167
11:44.840 --> 11:49.840
So what if we wanted to change this so that it's relative to our current main

168
11:50.570 --> 11:52.790
.py? Our main.py

169
11:53.350 --> 11:55.960
if we go to 'Show in Explorer',

170
11:56.290 --> 12:01.030
you can see that the path for this file is the following.

171
12:01.120 --> 12:01.720
It's under

172
12:01.720 --> 12:06.720
Users/LondonAppBrewery/PyCharmProjects/WindowsDemo.

173
12:07.780 --> 12:12.280
So that's the name of my project. And then afterwards,

174
12:12.340 --> 12:17.050
it's my main.py. So this is a bit of a tricky question.

175
12:17.470 --> 12:22.470
Given that this file is inside this current folder or whatever it is that you

176
12:23.170 --> 12:26.290
have as your folder which contains this file,

177
12:26.890 --> 12:31.890
how can you get it to use a relative file path so that it jumps back two folders

178
12:33.790 --> 12:38.790
to the LondonAppBrewery folder and then goes to Desktop and then to new file?

179
12:40.150 --> 12:44.590
On our Mac, it's a similar story. So we go to 'Reveal in Finder'.

180
12:44.620 --> 12:47.260
We can see this is where we are currently.

181
12:47.530 --> 12:52.530
So our current working directory is this day-24 project folder.

182
12:53.080 --> 12:57.040
So if we go one level up and the shortcut is command + up,

183
12:57.370 --> 13:02.260
we go to our day-24 folder. So this is our working directory.

184
13:02.740 --> 13:04.810
Now, if we want to go up one level,

185
13:04.810 --> 13:08.620
we can go to go, and then go to including folder,

186
13:08.860 --> 13:11.800
so one level up that gets us to PyCharm projects.

187
13:12.160 --> 13:16.690
And then if we go one level up further, we get to our Angela folder

188
13:17.080 --> 13:21.760
at which point we can then navigate downwards to Desktop/my_file.txt.

189
13:22.450 --> 13:23.770
This is your challenge.

190
13:23.980 --> 13:28.120
Figure out what the relative path to the text file on your desktop should be

191
13:28.390 --> 13:31.600
then modify the code and get it to work. Pause the video now.

192
13:34.600 --> 13:39.460
The way that we would modify this is in order to get to this Angela folder where

193
13:39.460 --> 13:42.940
we can continue to our Desktop and then to my file,

194
13:43.240 --> 13:45.130
we have to go up two levels.

195
13:45.190 --> 13:48.850
So that translates to ../../

196
13:48.940 --> 13:50.770
and now we're inside the Angela folder.

197
13:50.950 --> 13:55.750
So we can start going down. And PyCharm is smart enough to know what your

198
13:55.780 --> 13:57.220
folder structure looks like.

199
13:57.520 --> 14:01.960
So it will start giving you hints as to what folders are accessible at this

200
14:01.960 --> 14:02.793
level.

201
14:02.890 --> 14:07.420
So I can get to Desktop and then I can get to my_file.txt.

202
14:07.840 --> 14:10.060
And if I go ahead and run my code,

203
14:10.150 --> 14:15.070
you can see it's able to read and print the content of that file.

204
14:15.940 --> 14:20.650
The main difference between an absolute file path and a relative file path is

205
14:20.650 --> 14:25.650
the absolute file path is always relative to the root of your computer. On

206
14:26.710 --> 14:29.740
Windows that's the C drive, usually anyways

207
14:29.740 --> 14:32.290
unless you've changed it. And on a Mac,

208
14:32.380 --> 14:34.780
it is the Macintosh HD.

209
14:35.620 --> 14:40.210
Now the relative file path is relative to your current working directory.

210
14:40.210 --> 14:44.380
So it depends on where you are and where you're trying to get to. Now,

211
14:44.410 --> 14:49.360
depending on the situation and where that file you are interested in is located,

212
14:49.630 --> 14:53.870
you might decide to use the absolute file path or the relative file path.

213
14:54.550 --> 14:56.200
And you can see that in this case

214
14:56.200 --> 15:01.200
it's not necessarily that much shorter or that much more easier to understand.

215
15:01.840 --> 15:04.090
So if you know how to do both,

216
15:04.480 --> 15:08.050
then you can adapt to the situation and figure out which one is most

217
15:08.050 --> 15:12.130
appropriate. Now I know that paths can be confusing.

218
15:12.430 --> 15:16.330
So that's why I've come up with a whole bunch of quiz exercises for you in the

219
15:16.330 --> 15:20.530
next lesson for you to get more the practice with file paths so that you can

220
15:20.530 --> 15:22.540
feel confident leaving this lesson.

221
15:23.110 --> 15:26.140
So for all of that and more, I'll see you on the next lesson.