[issue894936] Have a split corresponding with os.path.join

R. David Murray report at bugs.python.org
Fri Jul 3 20:00:06 CEST 2009


R. David Murray <rdmurray at bitdance.com> added the comment:

I think the OP meant os.path.split, not str.split, but I don't
understand what incompatibility he is referring to.  This seems to work
fine:

>>> ntpath.join(*ntpath.split("Z:/xyz"))
'Z:/xyz'

That said, I'd like to note that I was surprised the first few times I
used os.path.split to find that it did only one split.  I fully expected
it to be symetric with os.path.join and analogous to str.split and give
me a list of path components.

After thinking about this for a while, I think there are enough
subleties involved in doing this operation that it would be worth having
a cannonical function to do it the "right way" (which, IMO, is not how
the proposed patch does it!)

Here is what I think it should produce:

  D:/abc\\xyz.ext   -->    ['D:\\', 'abc', 'xyz.ext']
  D:abc/xyz.ext     -->    ['D:', 'abc', 'xyz.ext']
  
I don't think this is what the OP is expecting, but I think it is
necessary to keep the drive attached to the first path element to avoid
ambiguity and to be consistent with the rest of the os.path semantics.

The reason I think this is worth doing is that otherwise what one would
do to produce the list of path components would be something like:

  drive, rpath = splitdrive(normpath("D:/abc/def.ext"))
  pathcomponents = rpath.split(sep)

which produces the result:

   ['', 'abc', 'def.ext']

when what one really wants is:

   ['\\', 'abc', 'def.ext']

Yes, your code can treat '' as indicating an absolute path, but that
adds one more step to the mutlistep process, the omission of any one of
which leads to subtle bugs.  Better to have a function that does it right.

Now, the question is, are there enough real use cases for this function
to motivate someone to do the work to add it?  I don't have any myself.

----------
nosy: +r.david.murray
priority: normal -> low
versions: +Python 3.2

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue894936>
_______________________________________


More information about the Python-bugs-list mailing list