clip_1.json opened, parsed and held a plausible byte count. It described clip_14.mp4. A Python script writes one JSON sidecar file per camera clip, and on a folder of fourteen clips it had written thirteen, with the first clip's sidecar now holding another clip's data.

The line responsible looked harmless: stem = name.rstrip('.mp4'). It reads like an instruction to take .mp4 off the end. That is not what it does. rstrip does not remove a suffix. It removes any run of trailing characters that appear anywhere in the string you pass it, in any order, so '.mp4' means: strip trailing dots, the letter m, the letter p and the digit 4. A clip whose name ends in a 4 loses the 4 as well, and the names start colliding.

Below: the failing script, what it wrote, four ways of dropping an extension compared, and the version I use now.

Every block was run with Python 3.10.12 on Linux, each set of scripts in a fresh empty folder. Every output is pasted as printed, and a second run in another fresh folder gave byte-identical output for all five. The clips are stand-in files the first script creates, so anyone can rerun this.

Fourteen clips, thirteen sidecars

import json, os os.makedirs("clips", exist_ok=True) os.makedirs("sidecars", exist_ok=True) for n in range(1, 15): with open(f"clips/clip_{n}.mp4", "wb") as f: f.write(b"\0" * n) # stand-in clip: size in bytes = clip number for name in sorted(os.listdir("clips")): stem = name.rstrip('.mp4') size = os.path.getsize(os.path.join("clips", name)) with open(f"sidecars/{stem}.json", "w") as f: json.dump({"clip": name, "bytes": size}, f) print("clips: ", len(os.listdir("clips"))) print("sidecars:", len(os.listdir("sidecars"))) 
clips: 14 sidecars: 13 

The script exited 0. A job that only checks the exit status calls this clean. One sidecar is missing, and nothing says which.

Which sidecar went missing

Reading every sidecar back and asking which clip it describes:

import json, os for side in sorted(os.listdir("sidecars")): with open(os.path.join("sidecars", side)) as f: data = json.load(f) print(f"{side:<14} holds {data['clip']}") described = set() for side in os.listdir("sidecars"): with open(os.path.join("sidecars", side)) as f: described.add(json.load(f)["clip"]) print("no sidecar:", sorted(set(os.listdir("clips")) - described)) 
clip_.json holds clip_4.mp4 clip_1.json holds clip_14.mp4 clip_10.json holds clip_10.mp4 clip_11.json holds clip_11.mp4 clip_12.json holds clip_12.mp4 clip_13.json holds clip_13.mp4 clip_2.json holds clip_2.mp4 clip_3.json holds clip_3.mp4 clip_5.json holds clip_5.mp4 clip_6.json holds clip_6.mp4 clip_7.json holds clip_7.mp4 clip_8.json holds clip_8.mp4 clip_9.json holds clip_9.mp4 no sidecar: ['clip_1.mp4'] 

Two things went wrong, and they are different failures.

clip_4.mp4 became clip_.json. Stripping trailing characters from the set ., m, p, 4 removes the 4, then the p, then the m, then the ., then the 4 that was part of the clip number. It stops at the underscore because the underscore is not in the set. The sidecar exists, but under a name that no other part of a pipeline will ever look up.

clip_14.mp4 became clip_1.json, overwriting the sidecar for clip_1.mp4. Same mechanism: the trailing 4 of 14 is stripped, and what is left is clip_1. Because the loop walks the folder in sorted order, clip_1.mp4 is written first and clip_14.mp4 overwrites it a few iterations later. Opening a file with mode 'w' truncates it if it already exists, which is exactly what you want for a fresh write and exactly what hides a collision.

That is the worst kind of wrong for metadata: clip_1.json exists, parses and holds a plausible size, just of a different clip.

Every other clip, 2 through 13 except 4, comes through fine. Most numbered names do not end in 4, so a quick test on three or four files can pass, and the bug only shows up once the counter reaches a number that ends in the wrong digit.

What the documentation says rstrip does

The Python documentation for str.rstrip is direct about it. The chars argument is a set, and the docs say the "argument is not a suffix; rather, all combinations of its values are stripped". It also points to the method that does what the line above was trying to do, describing str.removesuffix as "a method that will remove a single suffix string rather than all of a set of characters".

lstrip and strip work the same way from the left and from both ends. Their entries carry the same warning, with prefix in place of suffix.

So this is not a Python bug. The trap is that the name and the argument both suggest a suffix, and for most file names the two behaviors agree.

Four ways to drop an extension, side by side

Here is what each approach does to the same set of names, including a few that are not numbered clips:

import os from pathlib import Path names = ["clip_4.mp4", "clip_14.mp4", "clip_13.mp4", "garage_lamp.mp4", "backyard_camp.mp4", "door.MP4", "night.mp4.part"] print(f"{'name':<18} {'rstrip':<14} {'removesuffix':<14} {'splitext':<14} stem") for n in names: print(f"{n:<18} {n.rstrip('.mp4'):<14} {n.removesuffix('.mp4'):<14} " f"{os.path.splitext(n)[0]:<14} {Path(n).stem}") print() print("cam_courtyard.mp4".lstrip('cam_')) print("cam_courtyard.mp4".removeprefix('cam_')) 
name rstrip removesuffix splitext stem clip_4.mp4 clip_ clip_4 clip_4 clip_4 clip_14.mp4 clip_1 clip_14 clip_14 clip_14 clip_13.mp4 clip_13 clip_13 clip_13 clip_13 garage_lamp.mp4 garage_la garage_lamp garage_lamp garage_lamp backyard_camp.mp4 backyard_ca backyard_camp backyard_camp backyard_camp door.MP4 door.MP door.MP4 door door night.mp4.part night.mp4.part night.mp4.part night.mp4 night.mp4 ourtyard.mp4 courtyard.mp4 

Reading down the rstrip column:

  • Numbers ending in 4 lose a digit. clip_4 and clip_14 change; clip_13 does not.
  • Words ending in m or p lose letters. garage_lamp becomes garage_la and backyard_camp becomes backyard_ca. A footage folder named by location is as exposed as one named by number.
  • Upper-case extensions are not removed, but they are still damaged. door.MP4 comes back as door.MP. The 4 is in the set, so it goes, and the upper-case P is not, so stripping stops there.
  • A partial download is changed nowhere useful. night.mp4.part ends in letters that are not in the set, so rstrip returns it unchanged.

The other three columns:

  • removesuffix('.mp4') removes exactly that string if the name ends with it, and returns the name unchanged otherwise. It fixes the clip numbers and the lamp, but it is case-sensitive, so door.MP4 keeps its extension. It was added in Python 3.9, so code that has to run on older versions cannot use it.
  • os.path.splitext splits at the last dot of the file name. The docs describe the extension it returns as one that "is empty or begins with a period and contains at most one period". It does not care what the extension is, so door.MP4 becomes door. For night.mp4.part it returns night.mp4, which is the right answer if what you want is the name without the last extension, and the wrong answer if you assumed every file in the folder ends in .mp4.
  • Path(n).stem from pathlib gives the same results as splitext on every row here.

One more splitext detail that matters for camera folders copied off a phone: "Leading periods of the last component of the path are considered to be part of the root". A hidden file like .nomedia comes back with the whole name as its root and an empty extension, so an extension test like the one in the next section will not pick it up as a clip.

The last two lines of output show the same trap from the left. lstrip('cam_') on cam_courtyard.mp4 strips the c of courtyard too, because c is in the set, and returns ourtyard.mp4. removeprefix('cam_') returns courtyard.mp4.

The version I use now

The fixed script does three things the first one did not: it picks clips by their real extension, it checks that no two clips map to the same sidecar name before it writes anything, and it opens each sidecar in a mode that refuses to overwrite.

import json, os, shutil shutil.rmtree("sidecars", ignore_errors=True) os.makedirs("sidecars") clips = sorted(n for n in os.listdir("clips") if os.path.splitext(n)[1].lower() == '.mp4') stems = [os.path.splitext(n)[0] for n in clips] if len(set(stems)) != len(stems): raise SystemExit(f"two clips map to one sidecar name: {stems}") for name, stem in zip(clips, stems): size = os.path.getsize(os.path.join("clips", name)) with open(f"sidecars/{stem}.json", 'x') as f: # x: refuse to overwrite json.dump({"clip": name, "bytes": size}, f) print("clips: ", len(clips)) print("sidecars:", len(os.listdir("sidecars"))) with open("sidecars/clip_1.json") as f: print("clip_1.json holds", json.load(f)["clip"]) 
clips: 14 sidecars: 14 clip_1.json holds clip_1.mp4 

Fourteen in, fourteen out, and clip_1.json describes clip_1.mp4.

The choices in it:

  • os.path.splitext, not removesuffix. The extension test lowers the case, so an upper-case .MP4 from a different device is still treated as a clip, and the stem keeps whatever case the name had. A .part file is skipped, which is what I want for a file that is still being written.
  • The collision check runs before any file is written. If two clips ever produce the same stem, the script stops with a message that lists them, and the sidecar folder is left empty rather than half wrong. A wrong stem function is one way to get a collision, not the last; two folders merged into one, or names that differ only in case on a case-insensitive file system, can do it too.
  • Mode 'x' as a second guard. The open() documentation describes it as "open for exclusive creation, failing if the file already exists". Even if the check above were removed, a second write to the same sidecar name would now raise instead of silently replacing the first.

Here is that last guard on its own:

import os os.makedirs("out", exist_ok=True) for stem in ["clip_1", "clip_1"]: try: with open(f"out/{stem}.json", 'x') as f: f.write("{}") print("wrote ", stem) except FileExistsError as e: print("refused", stem, "->", type(e).__name__) 
wrote clip_1 refused clip_1 -> FileExistsError 

The second write raises FileExistsError. On a nightly job, a run that stops is better than a clean exit with a wrong file in place.

The script starts by deleting and recreating the sidecar folder, which is what makes 'x' usable on a rerun. If your job adds sidecars to a folder that already has some, you need to decide what a rerun should do with an existing one before switching modes.

What to check in your own code

  • Any strip, lstrip or rstrip with an argument longer than one character. A search for strip( followed by a quote and a dot finds the extension-shaped ones; a variable argument needs reading. If the argument was meant as a whole string to remove, it is almost certainly this bug. Use removesuffix or removeprefix for exact strings, and os.path.splitext or pathlib for file extensions.
  • Test with file names built to break the strip. A test folder of clip_1 to clip_3 will pass. Include a name ending in 4, one ending in m or p, and one with an upper-case extension.
  • Count outputs against inputs. One line comparing the number of clips with the number of sidecars would have caught the run above.
  • Write metadata with a mode that cannot overwrite when every output is meant to be new, so a collision becomes an error.

What I did not test

  • Windows. Every run was on Linux. splitext and pathlib follow Windows path rules on Windows, and a case-insensitive file system can turn two different stems into one file. I did not run any of this there.
  • Other Python versions. Only 3.10.12 was run. removesuffix and removeprefix need 3.9 or newer, per the docs.
  • Other tools in the pipeline. Shell parameter expansion and the basename command have their own suffix rules. This post only covers Python string methods.

Sources. Quotes come from the Python documentation pages Built-in Types (3.12 docs), os.path, common pathname manipulations and Built-in Functions, read on December 5, 2026.

The clips in a folder like this come from somewhere. Mine come from an old phone running Background Camera RemoteStream on Google Play, an Android app I make that records with the screen off. The string trap is the same for any folder of files a script renames.

The clips that lose their sidecar this way are clip_4, clip_14 and garage_lamp: names that end in the wrong character, and the ones a three-file test almost never contains.