Module 3 · Beginner · Files, folders and the terminal

Files, folders and file paths explained

Files, folders and file paths: how folders nest, absolute vs relative paths, Windows vs Mac and Linux paths, the current folder, and what . and .. mean.

By theprogrammers team · · 7 min read

You download marks.csv, write a Python script to read it, and run it. Python says FileNotFoundError: [Errno 2] No such file or directory: 'marks.csv'. The file is right there in your Downloads folder, so what's wrong? Python was looking somewhere else. To fix it, you need to understand files, folders and file paths: how a computer organises everything you store, and how to tell a program exactly where something is.

This is one of the most common stumbling blocks for new programmers, and it's not about code at all. Once you understand paths, a whole class of errors disappears, in Python, in the terminal and on servers.

What you need first: what is an operating system?

Key terms

  • File: a named piece of stored data, such as marks.csv or photo.jpg.
  • Folder (directory): a container that holds files and other folders.
  • Path: the address of a file or folder: the list of folders you go through to reach it.
  • Root: the top folder that contains everything else, like C:\ on Windows or / on Mac and Linux.
  • Absolute path: a full path starting from the root. It works from anywhere.
  • Relative path: a path starting from the folder you're currently in.
  • Current working directory: the folder a program or terminal is "in" right now.

Folders inside folders

Storage is organised like a family tree, upside down: one root at the top, folders inside it, folders inside those, and files at the ends. Here's part of a Windows laptop:

C:\
├── Program Files\
│   └── Python313\
└── Users\
    └── Ahana\
        ├── Desktop\
        ├── Documents\
        │   └── college\
        │       └── marks.csv
        └── Downloads\
            └── photo.jpg

And a similar Mac or Linux layout:

/
├── usr/
│   └── bin/
└── home/            (on a Mac this is /Users)
    └── ahana/
        ├── Documents/
        │   └── college/
        │       └── marks.csv
        └── Downloads/

Every file has exactly one place in this tree. Its path is the route from the top down to it.

Absolute paths

An absolute path starts at the root and lists every folder down to the file:

SystemAbsolute path to marks.csv
WindowsC:\Users\Ahana\Documents\college\marks.csv
macOS/Users/ahana/Documents/college/marks.csv
Linux/home/ahana/Documents/college/marks.csv

Read it like a postal address, from biggest to smallest: drive C, Users folder, Ahana's folder, Documents, college, then the file marks.csv.

Differences between systems:

  • Separators: Windows uses a backslash \; Mac and Linux use a forward slash /.
  • Drives: Windows starts with a drive letter (C:, D:). Mac and Linux have one tree starting at /, and extra drives appear as folders inside it.
  • Case: on Linux, Marks.csv and marks.csv are different files. On Windows and (by default) macOS they're the same.

Your home folder

Each user has a home folder for their own files: C:\Users\Ahana on Windows, /Users/ahana on Mac, /home/ahana on Linux. In the terminal on Mac and Linux, ~ (tilde) is a shortcut for it: ~/Documents means your Documents folder.

Relative paths and the current folder

Typing full paths every time would be tiring. A relative path starts from wherever you are now, the current working directory.

If you're in C:\Users\Ahana\Documents, then:

Relative pathPoints to
college\marks.csvC:\Users\Ahana\Documents\college\marks.csv
marks.csvC:\Users\Ahana\Documents\marks.csv (which doesn't exist)

Two special names help you move around:

NameMeansExample from Documents
.This folder.\college is the same as college
..The folder one level up (the parent)..\Downloads\photo.jpg reaches the Downloads folder

.. can be repeated: ..\.. goes up two levels.

Why your program couldn't find the file

Back to the opening problem. When you write open("marks.csv"), that's a relative path, so Python looks in its current working directory, which is usually the folder you ran the script from, not the folder the script is saved in, and not your Downloads folder.

Python can tell you where it's looking:

import os
print(os.path.isabs(os.getcwd()))
print(os.path.isabs("marks.csv"))
True
False

os.getcwd() ("get current working directory") returns an absolute path; we print a check here because yours will be different. "marks.csv" is relative, so Python joins it onto the working directory. Try print(os.getcwd()) yourself to see the real folder.

Three ways to fix a "file not found" error:

  1. Move the file into the folder you run the script from.
  2. Run the script from the right folder: in the terminal, cd into the file's folder first.
  3. Use the full absolute path in your code, or build the path from the script's own location (the Python pathlib lesson shows how).

Paths in Python

Python's pathlib module builds paths that work on every system, using / to join parts:

from pathlib import PurePosixPath, PureWindowsPath

print(PureWindowsPath("C:/Users") / "Ahana" / "Documents" / "marks.csv")
print(PurePosixPath("/home") / "ahana" / "Documents" / "marks.csv")

p = PureWindowsPath(r"C:\Users\Ahana\Documents\college\marks.csv")
print(p.name, "|", p.parent.name, "|", p.suffix)
C:\Users\Ahana\Documents\marks.csv
/home/ahana/Documents/marks.csv
marks.csv | college | .csv

PureWindowsPath and PurePosixPath show how the same path looks on each kind of system, whatever computer you run this on. In everyday code you just use Path, which picks the right style automatically.

The backslash trap in Python

In normal Python strings, a backslash starts a special code: \n is a new line and \t is a tab. So this Windows path goes wrong:

path = "C:\new_folder\table.csv"
print(path)
C:
ew_folder	able.csv

\n became a new line and \t became a tab. Fixes: use forward slashes ("C:/new_folder/table.csv", which Windows accepts), use a raw string with r in front (r"C:\new_folder\table.csv"), or use pathlib.

Naming files and folders well

Good names save you trouble in code and on servers:

  • Use lowercase, and hyphens or underscores instead of spaces: exam-marks-2026.csv, not Exam Marks (Final) 2026.csv. Spaces need quotes in the terminal and break some tools.
  • Avoid special characters like ? * : " < > |, which Windows doesn't allow.
  • Be descriptive: invoice-2026-12-04.pdf beats scan1.pdf.
  • Use dates as YYYY-MM-DD, so files sort in date order.
  • One project, one folder, with subfolders like data, notebooks and reports.

Hidden files

Some files are hidden by default, usually settings files. On Mac and Linux, any file whose name starts with a dot is hidden: .gitignore, .env, .venv. Programmers use these constantly, so turn on hidden files:

  • Windows File Explorer: View, Show, Hidden items.
  • macOS Finder: press Cmd+Shift+. (full stop).
  • Terminal: ls -a on Mac and Linux shows hidden files.

Common mistakes

  • Running a script from a different folder from the data it reads, causing FileNotFoundError.
  • Backslashes in Python strings turning into new lines and tabs.
  • Spaces in file names that break terminal commands.
  • Case mismatches that work on Windows and fail on Linux servers.
  • Saving files "somewhere" and losing them. Know your project folder.

Practice

  1. You're in /home/ahana/Documents. Write the relative path to /home/ahana/Downloads/photo.jpg.
Show the answer

../Downloads/photo.jpg. .. goes up from Documents to ahana, then down into Downloads.

  1. Is each path absolute or relative: C:\Windows, data/marks.csv, /etc/hosts, ../notes.txt, ~/Desktop?
Show the answer

Absolute: C:\Windows, /etc/hosts and ~/Desktop (the ~ expands to your home folder's full path). Relative: data/marks.csv and ../notes.txt.

  1. Rename My Project FINAL (2).py to something better.
Show the answer

Something like expense_tracker.py: lowercase, no spaces or brackets, and says what it is. Keep versions in Git instead of in file names.

  1. What does this print, and how do you fix it?
print("C:\temp\reports")
Show the answer

\t becomes a tab and \r is a carriage return, so it prints a garbled path instead of C:\temp\reports. The fix is a raw string or forward slashes:

print(r"C:\temp\reports")
print("C:/temp/reports")
C:\temp\reports
C:/temp/reports
  1. Using PureWindowsPath, get the file name, the extension and the parent folder of D:\projects\shop\sales_dec.xlsx.
Show the answer
from pathlib import PureWindowsPath

p = PureWindowsPath(r"D:\projects\shop\sales_dec.xlsx")
print(p.name, p.suffix, p.parent)
sales_dec.xlsx .xlsx D:\projects\shop

Key takeaways

  • Files live in a tree of folders, starting from a root (C:\ on Windows, / on Mac and Linux).
  • An absolute path starts from the root; a relative path starts from the current working directory.
  • . means this folder, .. means the folder above, and ~ means your home folder.
  • Windows uses \; Mac and Linux use /, and Linux file names are case-sensitive.
  • Most "file not found" errors come from running a program in a different folder from the file.

Frequently asked questions

What is the difference between a folder and a directory?

Nothing. "Folder" is the everyday word used in Windows and Mac; "directory" is the older technical word used in terminals and programming. They mean the same thing.

How do I copy a file's full path?

On Windows, Shift + right-click the file and choose "Copy as path". On a Mac, right-click, hold the Option key and choose "Copy ... as Pathname". In VS Code, right-click a file and choose "Copy Path".

Why does Python say file not found when the file exists?

Usually because the path is relative and Python's current working directory isn't the folder the file is in. Print os.getcwd() to see where Python is looking, then move the file, change folder, or use an absolute path.

What to learn next

  1. Learn what the end of a file name means in file types and extensions.
  2. Move between folders by typing in what is the terminal?
  3. Handle paths in code with Python pathlib.