Day 1 — Path Reading#

Goal: Master absolute paths, relative paths, and special shortcuts (~, ., ..) so you can always tell the system exactly where a file is — or figure out where you are.


What Is a Path?#

A path is the address of a file or folder on your system. Just like a street address tells you where a building is, a path tells the computer where to find a file.

/home/alice/projects/tds/data.csv

This path says: start at root (/), go into home, then alice, then projects, then tds, and there you’ll find data.csv.


Two Types of Paths#

1. Absolute Path — The Full Address#

An absolute path starts from the root / and gives the complete location.

/home/alice/projects/tds/data.csv
/etc/hosts
/usr/bin/python3
/tmp/scratch.txt

Key feature: An absolute path means the same thing no matter where you currently are. It’s like a full postal address — works from anywhere in the world.

2. Relative Path — Directions from Here#

A relative path starts from your current directory (where you are right now).

# If you are in /home/alice/projects/
tds/data.csv              # means /home/alice/projects/tds/data.csv
./tds/data.csv            # same thing (./ = current directory)
../alice/projects/        # go up one level, then back down

Key feature: A relative path changes meaning depending on where you are. It’s like saying “turn left, then right” — only works from a specific starting point.

🧠 Knowledge Check#

Q1: Which of the following is an absolute path?

  • A) ../data.csv
  • B) src/main.py
  • C) /home/alice/data.csv
  • D) ./script.sh
Answer

C — It starts with the root /, so it gives the complete location from the top of the filesystem.

Q2: When should you use a relative path?

  • A) When you want a path that works from any directory on the computer
  • B) When referring to a file in the same directory or a directory nearby, without writing out the full system path
  • C) Whenever you are using Python
  • D) When referring to system files in /etc
Answer

B — Relative paths are convenient for keeping paths short when working within a project folder, as they are relative to your current working directory.


Special Path Symbols#

SymbolMeaningExample
/Root of the filesystemls /
~Your home directorycd ~ = cd /home/yourname
.Current directory./script.sh = run script in current dir
..Parent directory (one level up)cd .. = go up
-Previous directory (with cd)cd - = go back to where you were

Walking Through Examples#

Starting position#

pwd
# /home/alice/projects/tds/week1

Going UP with ..#

cd ..
pwd
# /home/alice/projects/tds

cd ../..
pwd
# /home/alice/projects
# (went up TWO levels from tds → projects)

Going DOWN with folder names#

cd tds/week1
pwd
# /home/alice/projects/tds/week1

Combining UP and DOWN#

# From /home/alice/projects/tds/week1
# Go to /home/alice/projects/tds/week2
cd ../week2
pwd
# /home/alice/projects/tds/week2

Using ~ to jump home#

# From anywhere:
cd ~/projects
pwd
# /home/alice/projects

The pwd Command — “Where Am I?”#

When you are lost, always run:

pwd
# /home/alice/projects/tds/week1

pwd = Print Working Directory. It shows your current absolute path.

🧠 Knowledge Check#

Q1: You just opened a new terminal. You aren’t sure what folder you are in. What command do you type?

  • A) whereami
  • B) pwd
  • C) cd
  • D) ls
Answer

Bpwd (Print Working Directory) outputs the absolute path of your current location.


Path Reading — A Skill You’ll Use Constantly#

Every time you see a command like:

python3 src/main.py

Ask yourself:

  1. Where am I right now? (pwd)
  2. Is src/main.py relative or absolute?
  3. What is the full path?

If you’re in /home/alice/project/, then src/main.py means /home/alice/project/src/main.py.


Paths in Python#

Python also works with paths. Always use pathlib for cross-platform safety:

from pathlib import Path

# Current directory
here = Path(".")
print(here.resolve())  # shows absolute path

# Home directory
home = Path.home()
print(home)  # /home/alice

# Build paths safely (works on Windows AND Linux)
data_file = Path.home() / "projects" / "tds" / "data.csv"
print(data_file)  # /home/alice/projects/tds/data.csv

Never do this in Python:

open("C:\\Users\\alice\\data.csv")  # Windows-only, breaks on Linux

Always do this:

from pathlib import Path
open(Path.home() / "data.csv")     # works everywhere

🧠 Knowledge Check#

Q1: Why is using pathlib in Python better than hardcoding paths as strings like "C:\\data\\file.txt"?

  • A) Because strings are slow in Python
  • B) Because pathlib automatically downloads missing files
  • C) Because pathlib makes your code cross-platform, handling Windows and Linux path separators correctly
  • D) Because pathlib is required for reading CSV files
Answer

C — Hardcoding backslashes (\) makes your code Windows-only. pathlib ensures your code works across Linux, macOS, and Windows without modification.


Q&A#

Q: How do I tell if a path is absolute or relative?

A: If it starts with / (or ~ which expands to an absolute path), it’s absolute. If it starts with anything else (a folder name, ., ..), it’s relative.

/home/alice/data.csv    # absolute (starts with /)
~/data.csv              # absolute (~ expands to /home/alice)
data.csv                # relative
./data.csv              # relative (same as above)
../data.csv             # relative (parent directory)
Q: What does ./ mean at the start of a command?

A: ./ means “in the current directory.” It’s important for running scripts:

script.sh       # shell searches PATH for "script.sh" → probably not found
./script.sh     # explicitly runs the script.sh in THIS directory

Linux does NOT search the current directory by default (unlike Windows). You must use ./ to run a script in the current folder.

Q: Can I use ~ in Python code?

A: No — ~ is a shell feature, not a Python feature. In Python, use:

from pathlib import Path
home = Path.home()           # same as ~
config = Path("~").expanduser()  # also works
Q: What is the difference between . and ./?

A: They mean the same thing — the current directory. ./script.sh is just . (current directory) + / (path separator) + script.sh. Some commands require the ./ for clarity.


Exercises#

Exercise 1: Path identification

For each path, write whether it is absolute or relative:

  1. /etc/hosts
  2. data/raw.csv
  3. ~/projects/tds/
  4. ../../backup/
  5. ./run.sh
  6. /tmp/test.txt
Answers
  1. /etc/hostsAbsolute (starts with /)
  2. data/raw.csvRelative (starts with folder name)
  3. ~/projects/tds/Absolute (~ expands to /home/yourname)
  4. ../../backup/Relative (starts with ..)
  5. ./run.shRelative (starts with .)
  6. /tmp/test.txtAbsolute (starts with /)

Exercise 2: Path translation

You are currently in /home/alice/projects/tds/week1/. Write the relative path to reach each target:

  1. /home/alice/projects/tds/week2/data.csv
  2. /home/alice/notes.txt
  3. /home/alice/projects/tds/README.md
Answers

Starting from /home/alice/projects/tds/week1/:

  1. ../week2/data.csv — go up to tds/, then into week2/
  2. ../../../notes.txt — go up three levels: week1/ → tds/ → projects/ → alice/
  3. ../README.md — go up one level to tds/

Exercise 3: Navigate and verify

Run these commands and predict the output of each pwd:

cd ~
pwd                    # Q: What does this show?

cd /tmp
pwd                    # Q: What does this show?

cd -
pwd                    # Q: What does this show?

mkdir -p ~/test/a/b/c
cd ~/test/a/b/c
cd ../../
pwd                    # Q: What does this show?
Answers
cd ~
pwd                    # /home/yourname

cd /tmp
pwd                    # /tmp

cd -
pwd                    # /home/yourname  (cd - goes back to previous)

mkdir -p ~/test/a/b/c
cd ~/test/a/b/c
cd ../../
pwd                    # /home/yourname/test/a
                       # (from c/ → up to b/ → up to a/)

Clean up:

rm -r ~/test

Exercise 4: Windows-to-Linux path conversion

Convert these Windows paths to their Linux equivalents:

  1. C:\Users\john\Documents\project\main.py
  2. C:\Users\john\Desktop\
  3. C:\Windows\Temp\scratch.txt
Answers
  1. /home/john/Documents/project/main.py (or ~/Documents/project/main.py)
  2. /home/john/Desktop/ (or ~/Desktop/)
  3. /tmp/scratch.txt (Linux temp folder is /tmp, not /Windows/Temp)

Key changes: C:\Users\name/home/name, backslashes → forward slashes, drive letter removed.


Exercise 5: MCQ

Q1: You are in /home/bob/code/. What does cd ../data/ take you to?

  • A) /home/bob/data/
  • B) /home/data/
  • C) /home/bob/code/data/
  • D) /data/
Answer

A.. goes up one level from /home/bob/code/ to /home/bob/, then data/ takes you into /home/bob/data/.


Q2: Which of these is NOT a valid way to go to your home directory?

  • A) cd ~
  • B) cd $HOME
  • C) cd
  • D) cd /
Answer

Dcd / goes to the root of the filesystem, not your home directory. The other three all go to /home/yourname.


Q3: You run ./analyze.py and get “Permission denied.” What should you do?

  • A) Run sudo rm analyze.py
  • B) Run chmod +x analyze.py
  • C) Rename the file to analyze.sh
  • D) Run cd analyze.py
Answer

B — The file needs execute permission. chmod +x analyze.py adds it. Alternatively, you can run python3 analyze.py directly (Python interprets it; no execute permission needed for the file itself).