liveBook · Manning

Directory structure

As explained in Python How-To

A project folder structure is an organized arrangement of directories and files that groups related source artifacts within a software project.

A directory structure organizes related source artifacts and project data into predictable folders. As a project grows, separate folders for related files make code easier to read, maintain, and navigate. section 1.1.1

Create a portable data directory

Use pathlib to create a directory, verify it exists, and construct paths without hard-coding platform-specific separators: from pathlib import Path data_folder = Path("data") data_folder.mkdir() assert data_folder.exists() for subject_id in [123, 124, 125]: filepath = data_folder / f"subject_{subject_id}.dat" with open(filepath, "w") as file: file.write(f"Data for subject {subject_id}") The / operation works across Windows and macOS, unlike manually written paths such as data\\subject_123.dat. section 11.4.1

A useful directory workflow separates discovery, organization, and access:

  • Create missing nested folders with mkdir(parents=True, exist_ok=True). section 11.4.3
  • Find related files with glob, retrieve their names with name, and move them by renaming each path to a target path. section 11.4.3
  • For files inside subdirectories, use parent for the directory, stem for the filename without its extension, and suffix for the extension. section 11.5.1
Listing 11.16 Moving files to a target folder
subject_ids = [123, 124, 125]
data_folder = Path("data")
 
for subject_id in subject_ids:
    subject_folder = Path(f"subjects/subject_{subject_id}")
    subject_folder.mkdir(parents=True, exist_ok=True)          #1
    
    for subject_file in data_folder.glob(f"*{subject_id}*"):
        filename = subject_file.name                           #2
        target_path = subject_folder / filename                #3
        _ = subject_file.rename(target_path)
        print(f"Moving {filename} to {target_path}")
 
# output the following lines:
Moving subject_123.config to subjects/subject_123/subject_123.config
Moving subject_123.dat to subjects/subject_123/subject_123.dat
Moving subject_123.txt to subjects/subject_123/subject_123.txt
Moving subject_124.config to subjects/subject_124/subject_124.config
Moving subject_124.dat to subjects/subject_124/subject_124.dat
Moving subject_124.txt to subjects/subject_124/subject_124.txt
Moving subject_125.dat to subjects/subject_125/subject_125.dat
Moving subject_125.config to subjects/subject_125/subject_125.config
Moving subject_125.txt to subjects/subject_125/subject_125.txt

The worked listing groups each participant's configuration, data, and text files under subjects/subject_ID folders and prints the resulting moves. section 11.4.3

A well-structured project keeps related artifacts together while preserving consistent layouts across files. Regularly checking names, imports, file placement, and folder organization supports readable and maintainable code. section 1.1.1

How Python How-To covers it

How deep it goes
Explains creation, nesting, movement, discovery, and path metadata with pathlib, stopping short of broader project conventions.
Examples
Uses data and subjects/subject_ID folders containing .config, .dat, and .txt files from a scientific experiment.
What it assumes
Assumes basic Python file handling and teaches directory manipulation, path construction, and Path attributes.
What you can do after
Build it from scratch with pathlib, then organize, move, and locate related files programmatically.
How it connects
Connects directory structure with readable code, file paths, file metadata, globbing, and pathlib.

Also explained in

Compare how 7 books explain directory structure →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.