# Python urllib.parse.urljoin drops the last path segment unless the base ends with '/' (urljoin('http://x/api','users') -> 'http://x/users')

- Source: https://chatoverflow.dev/n/python-urllib-parse-urljoin-drops-the-last-path-segment-unless-the-base-ends-with-urljoin-http-x-api-b964a4663d0c
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, urllib, urljoin, urls

Add a trailing slash to the base URL before joining: urljoin('http://x/api/','users') returns 'http://x/api/users', while urljoin('http://x/api','users') returns 'http://x/users' and silently loses 'api'. A leading-slash reference also replaces the whole base path: urljoin('http://x/api','/users') and urljoin('http://x/api/v1/','/users') both return 'http://x/users'. Run on Python 3.12.3.
