Product case study · November 23, 2026

Ask Claude Code why removing .csv eats the filename

Repair a Python filename bug with Claude Code: reproduce rstrip data loss, define exact suffix removal and verify the fix with eight cases.

Reading time
5 min
Checked
Nov 23, 2026
An intact blue paper card and detached ochre tab sit above a shortened blue card with loose scraps on warm paper
A tiny string repair is only useful if it preserves the part you meant to keep.
Bottom line

Give the agent names that break the shortcut. Removing an exact suffix is a different job from trimming a set of characters.

Your export is called metrics.csv. A helper removes the extension and returns metri. That missing cs is a good reason to give a coding agent awkward examples before letting it tidy up a string function.

The tempting code is name.rstrip(".csv"). It reads like “remove .csv from the right.” Python reads it differently. This exercise turns that mismatch into a small Claude Code repair with a precise finish line: preserve every character except one exact, lowercase .csv at the end.

You need Python 3.9 or newer, Bash on Linux, macOS or WSL, and installed, signed-in Claude Code. The filenames below are fictional strings; this lab does not rename exports. The baseline and reference repair were run locally. The Claude prompt is an authored exercise, not a recorded agent run. Sources were checked November 23, 2026.

Catch the shortcut with an awkward name

Run:

python3 - <<'PY'
for name in ["report.csv", "metrics.csv", "status.csv"]:
    print(name, "->", name.rstrip(".csv"))
PY

The output is:

report.csv -> report
metrics.csv -> metri
status.csv -> statu

Python’s string documentation defines the argument to rstrip as a set of characters. It keeps removing trailing dots, c, s and v until it meets a character outside that set. After consuming .csv, it carries on into metrics.

A demo using only report.csv would hide the bug. I would keep that ordinary example, but never let it be the whole test.

Build a helper with a deliberately narrow contract

Create a fresh practice folder and the broken implementation:

suffix_lab=$(mktemp -d)
cd "$suffix_lab"
cat > labels.py <<'PY'
def export_label(name):
    return name.rstrip(".csv")
PY

Save this as SPEC.md:

export_label accepts a Python string and returns a string.
Remove exactly one lowercase .csv suffix when it is at the end.
Leave all other characters unchanged.
An absent suffix means return the input unchanged.
Do not lowercase, trim whitespace, parse paths or touch the filesystem.
The empty string remains empty; .csv alone becomes empty.
This is a label transformation, not filename validation or a rename tool.

The last two lines are choices for this exercise. A real importer might reject an empty label or accept uppercase extensions. Decide that separately; don’t let the agent silently invent those rules while fixing this bug.

Save the following as test_labels.py:

import unittest
from labels import export_label


class ExportLabels(unittest.TestCase):
    def test_ordinary_suffix(self):
        self.assertEqual(export_label("report.csv"), "report")

    def test_preserve_trailing_stem_characters(self):
        self.assertEqual(export_label("metrics.csv"), "metrics")

    def test_absent_suffix(self):
        self.assertEqual(export_label("metrics"), "metrics")

    def test_remove_only_one_suffix(self):
        self.assertEqual(export_label("report.csv.csv"), "report.csv")

    def test_uppercase_unchanged(self):
        self.assertEqual(export_label("report.CSV"), "report.CSV")

    def test_suffix_in_middle_unchanged(self):
        self.assertEqual(export_label("report.csv.backup"), "report.csv.backup")

    def test_suffix_only(self):
        self.assertEqual(export_label(".csv"), "")

    def test_empty(self):
        self.assertEqual(export_label(""), "")

Run python3 -m unittest -v test_labels. The local baseline ran eight tests with three failures: preserving the stem, leaving an absent suffix alone, and removing only one suffix. Those failures describe the repair more clearly than “fix the extension code.”

Give Claude the failing cases before the answer

Start claude in this practice directory. Check the displayed working directory and active permissions, then paste:

Read SPEC.md, labels.py and test_labels.py.
Run python3 -m unittest -v test_labels and report the actual baseline.
Explain why report.csv looks correct but metrics.csv does not.
Repair only labels.py to meet SPEC.md.
Keep all eight tests and the specification unchanged.
Do not install packages, rename files, access external services,
commit or delegate. This task transforms strings only.
Run the same tests after editing. Explain why your implementation
removes only one exact suffix and leaves absent suffixes unchanged.
If you cannot execute the tests, say so rather than predicting success.

Anthropic’s common workflows recommends giving Claude the error and reproduction context when debugging, then verifying the repair. The prompt’s file boundary is task scope; it does not establish a filesystem sandbox.

Watch for two plausible but wrong patches. replace(".csv", "") removes occurrences in the middle as well. Unconditional name[:-4] damages names without the suffix. Both can make the first example pass while failing the job you specified.

Check the patch without trusting the explanation

The reference implementation is:

def export_label(name):
    return name.removesuffix(".csv")

PEP 616 introduced exact prefix and suffix removal for Python 3.9, specifically addressing confusion around the older stripping methods. The reference repair passed all eight tests locally.

After Claude finishes, exit the session and run:

cat labels.py
python3 -m unittest -v test_labels

Compare the test file with the eight methods above. A green run after deleting the awkward cases proves little. The patch should contain a small string transformation, with no file operations or extra dependencies.

If you get an attribute error for removesuffix, run python3 --version in that same terminal. On an older interpreter, the explicit equivalent for this fixed suffix is name[:-4] if name.endswith(".csv") else name. If imports fail, check that both Python files are in your current directory and have the names shown above.

Use a path operation when you mean a path operation

This lab strips a known text ending. Replacing an arbitrary file extension is another job. Python’s pathlib documentation provides with_suffix for that purpose:

from pathlib import PurePosixPath

print(PurePosixPath("exports/metrics.csv").with_suffix(".json"))
# exports/metrics.json
print(PurePosixPath("exports/report.csv.backup").with_suffix(".json"))
# exports/report.csv.json

These pure path operations compute a new path; they do not rename a file. The second example replaces .backup, the final suffix. It does not search for .csv inside the name.

I would use the literal string method for this export-label contract and a path method for an actual extension change. Before turning either into a batch rename, write a separate rule for existing destinations and name collisions. Passing these eight string tests says nothing about whether overwriting a destination is acceptable.

Sources checked November 23, 2026

Put this to work

The difference between character trimming, literal suffix removal and path operations.

Try

Give Claude Code a broken export-label helper in a disposable folder.

Prove it worked

Re-run eight cases independently and inspect the one-function patch.

Keep in view

  • Expose why rstrip removes more than a file extension.
  • Keep eight acceptance cases fixed while Claude repairs one function.
  • Choose between exact text removal and path suffix replacement.
Learn the workflow: Set up a Claude Code practice session