Folders and paths

Day 2 — Write down where a file is

Data Analysis in Life Sciences

About this lecture

R will not click through folders for you. To read the seedling measurements it needs a written address: how do you write down exactly where a file is, so that it works on every computer?

Today you will:

  • draw the course project as a tree of folders;
  • write absolute and relative paths;
  • see why a relative path needs a starting folder;
  • check that saved work is on disk.

Tomorrow the terminal uses these paths; from week 2, R does.

A project is a tree

The course project folder contains data, output and reports; seedlings.csv is inside data and notes.txt inside reports.
  • data and reports are neighbours; their parent is the project folder.
  • The project folder is where every route in this project starts.

A path names each folder on the route

The path /home/anna/introcomputing-course/data/seedlings.csv drawn as a chain of folders from the top of the disk to the file.

This is how Ubuntu writes it. A Mac starts with /Users/anna/...; Windows writes C:\Users\anna\...\data\seedlings.csv.

Absolute and relative paths

Absolute: from the top of the disk.

/home/anna/introcomputing-course/data/seedlings.csv

Relative: from an agreed folder, here the project folder.

data/seedlings.csv

The same relative path works from the project folder but points to a missing folder from inside reports.

The folder a program starts from is its working directory.

Going up to the parent

The route from reports to data first goes up to the project folder, then down into data.

From reports, data is a neighbour: go up to the parent, then down.

Relative paths work on every computer

Anna’s and Ben’s absolute paths differ by username, but from the project folder both use data/seedlings.csv.

That is why our R code will say data/seedlings.csv.

Moving a folder changes the path

Moving map-a into map-b changes the path to note.txt from practice/map-a/note.txt to practice/map-b/map-a/note.txt; the contents stay the same.

Any route written before the move must be updated.

Saved work is on disk

Before closing, the edited note is in working memory; after closing, only the saved note on disk remains.

Unsaved text is in memory and disappears when the program closes. Saved work stays on disk.

Different computers, different folders

Laptop storage, local Ubuntu storage on Windows, and a remote Linux home folder are separate places.

A path is a place on one computer. A file is elsewhere only after you copy it there.

Check yourself

  1. From the project folder, what is the path to yesterday’s note?
  2. From inside reports, what does data/seedlings.csv point to?
  3. Why does C:\Users\anna\... fail on Ben’s laptop?
  4. You close the editor without saving. What is in the file?

Summary

  • A project is a tree; the project folder is the starting point.
  • A path lists the folders on the route to a file.
  • Absolute paths start at the top of the disk; relative paths start at the working directory.
  • Relative paths inside a project work on every computer.
  • Moving a folder changes the path, not the file. Saved work is on disk.

Next (Day 3, the terminal): the same folders, navigated by typing commands.

Now: open the Day 2 exercises. Leave with reports/day02-map.txt and the practice folder.