-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.qmd
More file actions
41 lines (25 loc) · 2.99 KB
/
paths.qmd
File metadata and controls
41 lines (25 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Paths
Inside your code, you may need to tell your machine where to find the files you want to work with. **File paths** are strings of characters that indicate where a path is saved in the file system of your machine. Different programming languages may deal with file paths a bit differently, but we can distinguish two types of file paths: **absolute paths**, and **relative paths**.
## Absolute file paths
These specify the **complete location of a file**, from the root folder of your hard drive to the file itself. For intance, this is an absolute file path:
```
C:/Users/Me/Documents/my-study/data/myfile.txt
```
While absolute paths do the job, their use is discouraged for **reproducibility issues**. In the preious example, our project project lives in the `my-study` folder, which is located in `C:/Users/Me/Documents/` in my machine. If our collaborator is using their own machine, their user might have a different name, like `Collab`, as opposed to our user `Me`,our script will no longer find `myfile.txt` where where the absolute path indicates, and will give our collaborator an error. Even if our users where named the same, our collaborator may have simply downloaded the `my-study` folder somewhere outside of their Documents folder, say for instance their Desktop (`C:/Users/Collab/Desktop/my-study`). The same error will be encountered.
One might be tempted to invite our poor collaborator to edit the paths in the script to find the files in their own computer. This is not good practice. Instead, one may use **relative paths**.
## Relative paths
Relative paths do not specify the complete path of the file. They rather assume that the user has moved their **working directory** to the folder where the script or the project lives in, or to some intermediate folder whose path is common for both us and our collaborator. This is a relative path, for instance:
```
my-study/data/myfile.txt
```
Assuming that both us and our collaborator have set their current working directory to be the `my-study` folder, wherever it may be located in their respective machines, our script will find the `myfile.txt` successfully in both cases. When you open your project as a folder in your IDE, most IDEs also move the working directory to such folder, which automatically makes relative paths work fine.
In conclusion, use relative paths whenever possible. Some packages exist so that using relative paths is even more convenient. In R, the package [**here**](https://here.r-lib.org/) allows you to simply write the following line (while at the same time taking care of some technical stuff associated with paths):
```r
library(here)
path <- here("data", "myfile.txt")
```
In Python, the [**pathlib**](https://docs.python.org/3/library/pathlib.html) standard module^[A standard module is simply a Python package that is included with a basic installation of Python, which means you do not have to install it before using it.] provides a similar convenience:
```python
from Pathlib import Path
path = Path("data", "myfile.txt")
```