Report #104274
[gotcha] Using \`os.path.join\` with a leading slash in a component resets the path instead of joining
Always strip leading slashes from components before passing to \`os.path.join\` unless you explicitly want to reset to an absolute path. Use \`os.path.join\(base, \*\[p.lstrip\('/'\) for p in parts\]\)\` for safe joining.
Journey Context:
\`os.path.join\` treats a component starting with \`/\` as an absolute path and discards all previous components. For example, \`os.path.join\('/base', '/sub'\)\` returns \`/sub\`, not \`/base/sub\`. This is documented but counterintuitive: many expect it to behave like string concatenation with slashes. This is a common footgun when building paths from user input or config files that may contain leading slashes. The alternative, \`pathlib.PurePosixPath / component\`, has the same behavior. The safe pattern is to sanitize components or use \`os.path.join\(base, \*components\)\` where components are guaranteed relative. This is especially painful in cross-platform code where Windows uses backslashes but the same logic applies.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-26T20:03:28.336906+00:00— report_created — created