liveBook · Manning

Directory structure

As explained in Learn AI-Assisted Python Programming, Second Edition

A directory is a filesystem container that organizes files and other directories by name.

A directory is a filesystem container that organizes files and other directories by name. A practical use is combining two picture directories into one while preserving the originals. section 9.5 section 9.5.1

A directory-based file merge separates the task into manageable steps:

  • Read each filename from the source directories with os.listdir. section 9.5.3
  • Compare each source file with files already in the target directory using filecmp.cmp. section 9.5.1
  • Copy only files without identical matches, using shutil.copy, and rename conflicting filenames when necessary. section 9.5.3
  • Test with disposable sample directories before trusting the result with important files. section 9.5.3

Merging two phone picture directories

Given pictures1 and pictures2, create pictures_combined. The example contains 210 PNG files, including 10 duplicates, so the result should contain one copy of each of 200 unique pictures. Copying rather than moving leaves both source directories intact if the program has a bug. section 9.5 section 9.5.1

Figure 9.3 Example of merging the photos in the pictures1 and pictures2 folders into the pictures–combined folder and managing the removal of duplicates
figure

Use the figure to visualize how pictures1 and pictures2 feed into pictures_combined while duplicate files are removed. section 9.5

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Explains directory listing, comparison, copying, duplicate handling, and testing, but stops short of filesystem internals.
Examples
Demonstrates merging pictures1 and pictures2 into pictures_combined while removing duplicate PNG files.
What you can do after
Build it from scratch using os, filecmp, and shutil to merge directories without duplicating identical files.
How it connects
Connects directories with files, file comparison, duplicate detection, filename handling, and Python modules.

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 Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.