Product case study · November 27, 2026

Ask Claude Code to keep stale files out of folder exports

Reproduce stale files surviving Python copytree, ask Claude Code for a fresh-folder export contract, and verify old output stays untouched.

Reading time
6 min
Checked
Nov 27, 2026
Paper folders separate a fresh pair of evidence cards from an older export containing an extra ochre card.
A successful copy can still leave you with the wrong set of files.
Bottom line

Give each export a new destination; reusing a folder makes yesterday's files part of today's handoff.

You remove an obsolete page from your source folder, run the export again, and hand over a folder that still contains it. The copy succeeded. The handoff is wrong.

This lab gives Claude Code a small but useful repair: make an exporter refuse a reused destination. I prefer a new folder for each review handoff because it makes the contents easier to account for and leaves earlier output available for comparison.

You need Bash, Python 3.8 or newer, and an installed, signed-in Claude Code session. The Python reproduction and reference repair were tested locally; the agent prompt is an authored exercise, not a recorded Claude run. Official sources checked November 27, 2026.

See what a successful copy leaves behind

Create a disposable practice directory and save the deliberately flawed exporter:

export_lab=$(mktemp -d /tmp/claude-export.XXXXXX)
cd "$export_lab"
cat > exporter.py <<'PY'
from shutil import copytree


def export_folder(source, destination):
    return copytree(source, destination, dirs_exist_ok=True)
PY

Run this small reproduction. It creates and cleans up its own sample files:

python3 - <<'PY'
from pathlib import Path
from tempfile import TemporaryDirectory
from exporter import export_folder

with TemporaryDirectory() as folder:
    root = Path(folder)
    source = root / "source"
    output = root / "output"
    source.mkdir()
    output.mkdir()
    (source / "current.txt").write_text("new", encoding="utf-8")
    (output / "obsolete.txt").write_text("old", encoding="utf-8")
    export_folder(source, output)
    print(sorted(path.name for path in output.iterdir()))
PY

The baseline prints:

['current.txt', 'obsolete.txt']

Python documents that dirs_exist_ok=True permits existing directories and overwrites corresponding files. It does not promise to remove destination-only files. Our reproduction shows why that distinction matters for an export.

Decide what “export” means

Save this contract as SPEC.md:

export_folder(source, destination) copies a trusted local source tree
of regular files and directories into a destination that does not exist.
Preserve file bytes and relative directory structure.
Return the destination path on success.
If the destination already exists, raise FileExistsError and leave
its contents untouched, even when it is an empty directory.
Do not delete, merge into, or silently rename an existing destination.
Do not change the source. Only exporter.py may change.
Add no dependencies. Keep these tests and this specification unchanged.
Symlinks, concurrent writers, metadata fidelity, and atomic publication
are outside this exercise. Source and destination must be separate trees.

The important decision is what happens on a second run. This contract asks the caller to choose a fresh path. A tool meant to synchronize a directory would need a different contract, including explicit rules for deletion.

Keep the acceptance tests fixed

Save this as test_exporter.py. The tests use Python’s temporary-directory context manager, so each case owns its sample data and cleans it up afterward.

from pathlib import Path
from tempfile import TemporaryDirectory
import unittest
from exporter import export_folder


def contents(folder):
    return {
        str(path.relative_to(folder)): path.read_bytes()
        for path in folder.rglob("*") if path.is_file()
    }


class ExportTests(unittest.TestCase):
    def setUp(self):
        temp = TemporaryDirectory()
        self.addCleanup(temp.cleanup)
        self.root = Path(temp.name)
        self.source = self.root / "source"
        self.source.mkdir()
        (self.source / "nested").mkdir()
        (self.source / "current.txt").write_bytes(b"new")
        (self.source / "nested" / "data.bin").write_bytes(b"\x00\xff")
        self.output = self.root / "output"

    def test_fresh_destination_copies_contents(self):
        before = contents(self.source)
        result = export_folder(self.source, self.output)
        self.assertEqual(Path(result), self.output)
        self.assertEqual(contents(self.output), before)
        self.assertEqual(contents(self.source), before)

    def test_existing_output_is_untouched(self):
        self.output.mkdir()
        (self.output / "current.txt").write_bytes(b"previous")
        (self.output / "obsolete.txt").write_bytes(b"keep")
        before = contents(self.output)
        with self.assertRaises(FileExistsError):
            export_folder(self.source, self.output)
        self.assertEqual(contents(self.output), before)

    def test_existing_empty_directory_is_rejected(self):
        self.output.mkdir()
        with self.assertRaises(FileExistsError):
            export_folder(self.source, self.output)
        self.assertEqual(list(self.output.iterdir()), [])

    def test_empty_source_creates_empty_destination(self):
        empty = self.root / "empty"
        empty.mkdir()
        export_folder(empty, self.output)
        self.assertTrue(self.output.is_dir())
        self.assertEqual(list(self.output.iterdir()), [])

Run:

python3 -m unittest -v test_exporter

Locally, the baseline ran four tests and failed two: both cases with an existing destination. The fresh-copy and empty-source cases passed. These checks use unittest assertions to test the contract rather than a particular line of implementation.

Give Claude Code a bounded repair

Start claude from this practice folder and send:

Read SPEC.md, exporter.py, and test_exporter.py.
Run python3 -m unittest -v test_exporter and explain the failures.
Repair only exporter.py to meet SPEC.md. Keep the tests unchanged.
Do not delete existing output or add a cleanup step before copying.
Rerun the tests. Report the exact command, outcome, and file diff.
Explain what the helper does on a second call to the same destination.

This follows the reproduce, repair, and verify pattern in Claude Code’s common workflows. If you need help opening the session and reviewing permissions, use the first-project lesson. The prompt defines the task’s scope; it does not create a filesystem sandbox.

If the agent suggests deleting the destination first, point it back to the preservation requirement. If it changes the tests to permit merging, restore the test file above. If it reports an import error, check that the shell is in the folder containing both Python files.

Review the small repair

This reference implementation passed all four tests locally:

from shutil import copytree


def export_folder(source, destination):
    return copytree(source, destination)

The default rejects an existing destination. There is no need to delete anything or add a separate existence check. After the agent finishes, run the test command yourself and inspect the diff. Rerunning the earlier reproduction should now raise FileExistsError; that is the intended rejection, not a broken tutorial.

For a real review handoff, choose a new output directory, wait for the copy to finish, and compare the resulting file list and contents before sharing it. A copy failure can leave a partial new directory. This helper is not an atomic publishing system, and a source changing during copying needs additional coordination.

Keep the old export. Give the next run a new destination and a clearly identified result to review.

Sources

Checked November 27, 2026:

Put this to work

Why copying into an existing directory does not make it match the source.

Try

Ask Claude Code to repair a one-function exporter in a disposable practice folder.

Prove it worked

Run four fixed tests before and after the repair, including an unchanged old export.

Keep in view

  • Reproduce an old file surviving a folder copy.
  • Require a new destination instead of silently merging exports.
  • Verify file contents and preserve earlier output.
Learn the workflow: Start a Claude Code project session