On Wed, Jan 06, 2021 at 07:07:30AM +0300, Mikhail V wrote:
I'd like to have an option to force the path separator for the "os.path.join()" method. E.g. if I run the script on Windows, but I generate, say, an URL, I'd find it convenient to use the same method, but with an explicit flag to "join" with the forward slash (because URLs use it).
"I don't care about correctness, I want to add arbitrary functionality to unrelated functions because it's convenient."
The whole point of using os.path.join is that you don't care what the separator is, so long as it is correct *for the OS at runtime*.
But for URLs, the separator never depends on the runtime OS. It is either fixed, or at worst will depend on the protocol.
URLs are also a lot more complicated than file paths, so you should be using urllib to assemble the parts:
If you can't be bothered (and let's be honest, we've all been there) there's nothing wrong with assembling URL path components with pure string operations. All you need is a one-liner:
def join(*parts, sep='/'): return sep.join([part.strip(sep) for part in parts])