Beginner 15 min Mac & PC

Understanding File Paths

Navigate your computer like a pro and troubleshoot 'file not found' errors with confidence.

What You'll Learn:

  • Understand absolute vs. relative file paths
  • Navigate using command line path syntax
  • Troubleshoot common path-related errors
  • Use paths effectively in Python and VS Code

File paths are like addresses for your filesβ€”they tell your computer exactly where to find something. Understanding paths is crucial for working with Python scripts, VS Code projects, and any command-line tools in digital humanities.

What Are File Paths?

A file path is the complete location of a file or folder on your computer. Think of it like a postal address that gets more specific as you read from left to right.

🏠 Path as Address Analogy

Postal Address:
123 Main Street
Springfield, IL 62704
United States
File Path:
/Users/student/Documents/DH-Projects/research-notes.txt

Absolute vs. Relative Paths

Understanding the difference between absolute and relative paths is fundamental to working effectively with files in programming and research.

Absolute Paths

An absolute path gives the complete location from the root of your computer.

Mac Examples:

/Users/sarah/Documents/DH-Projects/victorian-novels/data.csv
/Applications/Visual Studio Code.app
/System/Library/Fonts/Arial.ttf

PC Examples:

C:\Users\Sarah\Documents\DH-Projects\victorian-novels\data.csv
C:\Program Files\Microsoft VS Code\Code.exe
D:\External Drive\Backup\research-archive.zip

Relative Paths

A relative path gives the location relative to where you currently are (your β€œworking directory”).

🧭 Interactive: Identify Path Types

Can you identify whether these paths are absolute or relative?

/home/student/thesis.txt
../images/manuscript.jpg
C:\Users\John\Desktop\script.py
data/processed/results.csv

Platform Differences

Mac and Linux Paths

  • Start with / (forward slash)
  • Use / to separate folders
  • Case-sensitive (usually)
  • Root directory is /

Windows Paths

  • Start with drive letter (C:, D:, etc.)
  • Use \ (backslash) to separate folders
  • Case-insensitive
  • Root directory is drive letter (C:\)

Quick Check: Platform Recognition

Which path belongs to a Windows computer?

Special Path Symbols

  • . (single dot) = current directory
  • .. (double dot) = parent directory
  • ~ (tilde) = home directory (Mac/Linux)
  • %USERPROFILE% = home directory (Windows)

Examples in Context

If you’re currently in /Users/sarah/DH-Projects/novels/:

You are here: /Users/sarah/DH-Projects/novels/
. β†’ /Users/sarah/DH-Projects/novels/ (current directory)
.. β†’ /Users/sarah/DH-Projects/ (go up one level)
../poetry/ β†’ /Users/sarah/DH-Projects/poetry/ (up one, then into poetry)
./dickens/ β†’ /Users/sarah/DH-Projects/novels/dickens/ (into dickens subfolder)

πŸ–₯️ Practice Navigation

Try these commands to practice path navigation:

pwd ← Shows current directory
ls . ← List current directory
ls .. ← List parent directory
cd ../ ← Move to parent directory

Paths in Programming and DH Tools

Python Path Issues

Common problems and solutions:

❌ Common Python Path Error

```python # This often fails: with open('data.csv', 'r') as file: content = file.read() # Error: FileNotFoundError: [Errno 2] No such file or directory: 'data.csv' ```

βœ… Better Approaches

```python import os # Option 1: Use absolute path file_path = '/Users/sarah/DH-Projects/data/data.csv' with open(file_path, 'r') as file: content = file.read() # Option 2: Build path relative to script location script_dir = os.path.dirname(os.path.abspath(__file__)) file_path = os.path.join(script_dir, 'data', 'data.csv') with open(file_path, 'r') as file: content = file.read() ```

VS Code and Path Issues

πŸ’‘ VS Code Path Tips

  • Open folder, not files: Always open your project folder in VS Code, not individual files
  • Integrated terminal: Uses your project folder as working directory
  • Relative paths: Work consistently when folder is properly opened
  • Python path: Make sure VS Code detects your Python interpreter correctly

πŸ› Debug the Path Problem

A student is getting a "file not found" error. Can you identify what's wrong?

Project Structure:
my-research/
β”œβ”€β”€ scripts/
β”‚   └── analyze.py
β”œβ”€β”€ data/
β”‚   └── texts.csv
└── results/
      
Code in analyze.py:
import pandas as pd

# This line is failing:
df = pd.read_csv('texts.csv')
print(df.head())
What's the problem?

Troubleshooting Path Problems

Common Error Messages and Solutions

🚨 "FileNotFoundError" or "No such file or directory"

Check these:
  • Is the filename spelled correctly?
  • Are you in the right directory?
  • Does the file actually exist where you think it does?
  • Are you using the right type of slash (/ vs \)?

🚨 "Permission denied"

Try these:
  • Check if file is open in another program
  • Verify you have read/write permissions
  • On Mac: use ls -la to check permissions
  • On PC: right-click β†’ Properties β†’ Security

🚨 Paths with spaces causing issues

Solutions:
  • Put quotes around the entire path: "My Documents/file.txt"
  • Escape spaces with backslash: My\ Documents/file.txt
  • Use tab completion to avoid typing spaces
  • Better: avoid spaces in file/folder names

Hands-On Practice

Exercise 1: Path Translation

πŸ“ Convert Between Path Types

Given this file structure, practice writing different types of paths:

/Users/alex/DH-Research/
β”œβ”€β”€ 01-data/
β”‚   β”œβ”€β”€ raw/
β”‚   β”‚   └── survey-responses.csv
β”‚   └── processed/
β”‚       └── clean-data.csv
β”œβ”€β”€ 02-scripts/
β”‚   β”œβ”€β”€ clean-data.py
β”‚   └── analyze.py
└── 03-outputs/
    └── results.txt
    
Question 1:

You're in the 02-scripts/ folder. Write a relative path to access survey-responses.csv:

Question 2:

Write the absolute path to clean-data.py (assuming you're Alex):

Question 3:

You're in 01-data/processed/. Write a relative path to results.txt:

Exercise 2: Fix the Python Script

🐍 Fix the Path Issues

This Python script has path problems. Can you fix them?

Project structure:
text-analysis/
β”œβ”€β”€ main.py
β”œβ”€β”€ data/
β”‚   └── novels.txt
└── utils/
    └── helpers.py
    
Broken code in main.py:
import sys
sys.path.append('helpers.py')  # Problem 1
from helpers import clean_text

# Problem 2
with open('novels.txt', 'r') as f:
    text = f.read()

cleaned = clean_text(text)
print(cleaned)
Your Fix:

Advanced Path Concepts

Environment Variables

Some useful environment variables for paths:

Mac/Linux:

  • $HOME - Your home directory
  • $PWD - Current working directory
  • $PATH - Directories searched for commands

Windows:

  • %USERPROFILE% - Your home directory
  • %CD% - Current directory
  • %PATH% - Directories searched for commands

Path Libraries in Python

🐍 Modern Python Path Handling

```python from pathlib import Path # Create path objects project_root = Path.home() / 'DH-Research' data_file = project_root / 'data' / 'texts.csv' # Check if path exists if data_file.exists(): print(f"Found file: {data_file}") # Get parent directory data_dir = data_file.parent # Get just the filename filename = data_file.name # Cross-platform path building output_file = project_root / 'results' / 'analysis.txt' ```

Knowledge Check

Proficiency Check: File Paths

You're working in /Users/student/DH-Project/analysis/ and need to access a file at /Users/student/DH-Project/data/raw/manuscript.txt. What's the shortest relative path?

Quick Reference

πŸ“‹ Path Quick Reference

Absolute Path Indicators:
  • Mac/Linux: starts with /
  • Windows: starts with drive letter C:\
Navigation Shortcuts:
  • . = current directory
  • .. = parent directory
  • ~ = home directory (Mac/Linux)
Python Path Handling:
  • os.path.join() for cross-platform paths
  • pathlib.Path() for modern Python
  • os.path.abspath(__file__) for script location

Understanding file paths is crucial for effective digital humanities work. With these concepts learned, you’re ready to learn about working with compressed files, which will expand your file management toolkit.