On 26/05/2020 18:51, Mats Wichmann wrote: > On 5/26/20 10:57 AM, BlindAnagram wrote: >> On 26/05/2020 16:59, Mats Wichmann wrote: >>> On 5/26/20 8:56 AM, BlindAnagram wrote: >>>> I came across an issue that I am wondering whether I should report as an >>>> issue. If I have a directory, say: >>>> >>>> base='C:\\Documents' >>>> >>>> and I use os.path.join() as follows: >>>> >>>> join(base, '..\\..\\', 'build', '') >>>> >>>> I obtain as expected from the documentation: >>>> >>>> 'C:\\Documents\\..\\..\\build\\' >>>> >>>> But if I try to make the directory myself (as I tried first): >>>> >>>> join(base, '..\\..\\', 'build', '\\') >>>> >>>> I obtain: >>>> >>>> 'C:\\' >>>> >>>> The documentation says that an absolute path in the parameter list for >>>> join will discard all previous parameters but '\\' is not an absoute path! >>> >>> But it is - an absolute path is one that starts with the pathname separator. >> >> On Windows, when part of a string representing a file or directory path, >> '\\' acts as a directory separator. It may imply, as you say, that it >> is a path on the current drive but it also behaves as a move to a >> sub-directory of what has appeared to its left. And this is how it is >> treated in other os.path functions. For example: >> >> base = "C:\\Documents\\Finance\\" >> abspath(base) >> 'C:\\Documents\\Finance' >> >> where it can be seen that '\\' is being treated as a directory >> separator. In my view it is not sensible to have some functions >> treating '\\' in one way and others differently. I would hence expect >> join() to accept '\\' as an addition to a file path on Windows and not >> a signal of an absolute path. > > Unlike the string join method, which would behave as you seem to want, > the os.path.join method "knows" it is working on paths, so it's going to > assign some meaning to the content of the pieces. If it was up to me I > might not have chosen the approach Python did of "if we see a piece that > looks like an absolute path, discard what came before", but that's the > one that was chosen, presumably for good reasons, and it is so > documented. And the pathlib library module chose to retain that > behavior: "When several absolute paths are given, the last is taken as > an anchor (mimicking os.path.join()’s behaviour)". So it's just > something to get used to?
I agree with much of what you say. But I believe that it is more natural and less 'dangerous' when a functions behaviour matches that which its name implies, that is, to join things together, not to throw one of them away! And, I don't presume that there were good reasons since it might simply have been an oversight, which is why I raised it (even Python developers are not perfect :-) -- https://mail.python.org/mailman/listinfo/python-list