You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note that there is no forward slash at the beginning of a relative path; if we accidentally typed `"/data/happiness_report.csv"`,
126
+
Python would look for a folder named `data` in the root folder of the computer—but that doesn't exist!
126
127
127
-
+++
128
+
Aside from specifying places to go in a path using folder names (like `data` and `worksheet_02`), we can also specify two additional
129
+
special places: the *current directory* and the *previous directory*. We indicate the current working directory with a single dot `.`, and
130
+
the previous directory with two dots `..`. So for instance, if we wanted to reach the `bike_share.csv` file from the `worksheet_02` folder, we could
131
+
use the relative path `../tutorial_01/bike_share.csv`. We can even combine these two; for example, we could reach the `bike_share.csv` file using
132
+
the (very silly) path `../tutorial_01/../tutorial_01/./bike_share.csv` with quite a few redundant directions: it says to go back a folder, then open `tutorial_01`,
133
+
then go back a folder again, then open `tutorial_01` again, then stay in the current directory, then finally get to `bike_share.csv`. Whew, what a long trip!
128
134
129
-
So which one should you use? Generally speaking, to ensure your code can be run
130
-
on a different computer, you should use relative paths. An added bonus is that
131
-
it's also less typing! Generally, you should use relative paths because the file's
132
-
absolute path (the names of
133
-
folders between the computer's root `/` and the file) isn't usually the same
134
-
across different computers. For example, suppose Fatima and Jayden are working on a
135
-
project together on the `happiness_report.csv` data. Fatima's file is stored at
135
+
So which kind of path should you use: relative, or absolute? Generally speaking, you should use relative paths.
136
+
Using a relative path helps ensure that your code can be run
137
+
on a different computer (and as an added bonus, relative paths are often shorter—easier to type!).
138
+
This is because a file's relative path is often the same across different computers, while a
139
+
file's absolute path (the names of
140
+
all of the folders between the computer's root, represented by `/`, and the file) isn't usually the same
141
+
across different computers. For example, suppose Fatima and Jayden are working on a
142
+
project together on the `happiness_report.csv` data. Fatima's file is stored at
136
143
137
144
```
138
145
/home/Fatima/project/data/happiness_report.csv
@@ -150,16 +157,19 @@ their different usernames. If Jayden has code that loads the
150
157
`happiness_report.csv` data using an absolute path, the code won't work on
151
158
Fatima's computer. But the relative path from inside the `project` folder
152
159
(`data/happiness_report.csv`) is the same on both computers; any code that uses
153
-
relative paths will work on both!
160
+
relative paths will work on both! In the additional resources section,
161
+
we include a link to a short video on the
162
+
difference between absolute and relative paths.
154
163
155
164
```{index} URL
156
165
```
157
166
158
-
Your file could be stored locally, as we discussed, or it could also be
159
-
somewhere on the internet (remotely). For this purpose we use a
167
+
Beyond files stored on your computer (i.e., locally), we also need a way to locate resources
168
+
stored elsewhere on the internet (i.e., remotely). For this purpose we use a
160
169
*Uniform Resource Locator (URL)*, i.e., a web address that looks something
161
-
like https://google.com/. URLs indicate the location of a resource on the internet and
162
-
helps us retrieve that resource.
170
+
like https://datasciencebook.ca/. URLs indicate the location of a resource on the internet, and
171
+
start with a web domain, followed by a forward slash `/`, and then a path
172
+
to where the resource is located on the remote machine.
163
173
164
174
## Reading tabular data from a plain text file into Python
0 commit comments