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
If we instead wanted to use a relative path, we would need to list out the sequence of steps needed to get from our current
117
-
working directory to the file, with slashes `/` separating each step. Since we are currently in the `worksheet_02` folder,
116
+
working directory to the file, with slashes `/` separating each step. Since we are currently in the `project3` folder,
118
117
we just need to enter the `data` folder to reach our desired file. Hence the relative path is `data/happiness_report.csv`,
119
118
and we can load the file using its relative path as a string passed to `read_csv`.
120
119
```python
@@ -132,12 +131,12 @@ Python would look for a folder named `data` in the root folder of the computer&m
132
131
```{index} see: .; path
133
132
```
134
133
135
-
Aside from specifying places to go in a path using folder names (like `data` and `worksheet_02`), we can also specify two additional
134
+
Aside from specifying places to go in a path using folder names (like `data` and `project3`), we can also specify two additional
136
135
special places: the *current directory* and the *previous directory*. We indicate the current working directory with a single dot `.`, and
137
-
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
138
-
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
139
-
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`,
140
-
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!
136
+
the previous directory with two dots `..`. So for instance, if we wanted to reach the `bike_share.csv` file from the `project3` folder, we could
137
+
use the relative path `../project2/bike_share.csv`. We can even combine these two; for example, we could reach the `bike_share.csv` file using
138
+
the (very silly) path `../project2/../project2/./bike_share.csv` with quite a few redundant directions: it says to go back a folder, then open `project2`,
139
+
then go back a folder again, then open `project2` again, then stay in the current directory, then finally get to `bike_share.csv`. Whew, what a long trip!
141
140
142
141
So which kind of path should you use: relative, or absolute? Generally speaking, you should use relative paths.
143
142
Using a relative path helps ensure that your code can be run
@@ -149,20 +148,20 @@ across different computers. For example, suppose Fatima and Jayden are working o
149
148
project together on the `happiness_report.csv` data. Fatima's file is stored at
150
149
151
150
```
152
-
/home/Fatima/project/data/happiness_report.csv
151
+
/home/Fatima/project3/data/happiness_report.csv
153
152
```
154
153
155
154
while Jayden's is stored at
156
155
157
156
```
158
-
/home/Jayden/project/data/happiness_report.csv
157
+
/home/Jayden/project3/data/happiness_report.csv
159
158
```
160
159
161
160
Even though Fatima and Jayden stored their files in the same place on their
162
161
computers (in their home folders), the absolute paths are different due to
163
162
their different usernames. If Jayden has code that loads the
164
163
`happiness_report.csv` data using an absolute path, the code won't work on
165
-
Fatima's computer. But the relative path from inside the `project` folder
164
+
Fatima's computer. But the relative path from inside the `project3` folder
166
165
(`data/happiness_report.csv`) is the same on both computers; any code that uses
167
166
relative paths will work on both! In the additional resources section,
0 commit comments