TracksPractical Coding FoundationsWorking With FilesUnderstanding File Paths(2 of 9)

Understanding File Paths

A file path is the address of a file on your computer. Just like a street address helps you find a building, a file path helps your program find a file. Getting paths right is essential — a wrong path means your program can't find the data it needs.

Absolute Paths

An absolute path specifies the complete location from the root of your file system. It works from anywhere because it includes every folder from the top down.

/Users/alice/projects/myapp/data.txt

Absolute paths on Unix-like systems start with / (the root).

C:\Users\alice\projects\myapp\data.txt

Absolute paths on Windows start with a drive letter like C:\.

Absolute paths are unambiguous but brittle — they break if you move your project or share it with someone whose folders are organized differently.

Relative Paths

A relative path describes a file's location relative to your program's current working directory — the folder where your program is running.

# Same folder as your program
"data.txt"

# Inside a subfolder
"data/input.txt"

# Parent folder (one level up)
"../output.txt"

# Two levels up, then into another folder
"../../shared/config.txt"

The .. means "go up one folder." The . means "current folder" (often optional).

Relative paths are portable. If you move your entire project folder, relative paths still work because they describe relationships between files, not fixed locations.

Current Working Directory

The current working directory is where your program "thinks" it is. When you run a Python script, this is typically the folder containing that script — but not always.

import os
print(os.getcwd())  # Shows current working directory

Understanding this helps debug "file not found" errors. Your path might be correct relative to one folder but wrong relative to where the program actually runs.

Cross-Platform Paths

Windows uses backslashes (\), while macOS and Linux use forward slashes (/). Python's os.path module handles this automatically:

import os

# Works on any operating system
path = os.path.join("data", "input.txt")
print(path)  # data/input.txt or data\input.txt

Using os.path.join() makes your code work across different operating systems.

See More

Further Reading

You need to be signed in to leave a comment and join the discussion