
Currently, os.path.join joins strings specified in its arguments, with one string per argument. On its own, that is not a problem. However, it is inconsistent with str.join, which accepts only a list of strings. This inconsistency can lead to some confusion, since these operations that have similar names and carry out similar tasks have fundamentally different syntax. My suggestion is to allow os.path.join to accept a list of strings in addition to existing one string per argument. This would allow it to be used in a manner consistent with str.join, while still allowing existing code to run as expected. Currently, when os.path.join is given a single list, it returns that list exactly. This is undocumented behavior (I am surprised it is not an exception). It would mean, however, this change would break code that wants a list if given a list but wants to join if given multiple strings. This is conceivable, but outside of catching the sorts of errors this change would prevent, I would be surprised if it is a common use-case. In the case where multiple arguments are used and one or more of those arguments are a list, I think the best solution would be to raise an exception, since this would avoid corner cases and be less likely to silently propagate bugs. However, I am not set on that, so if others prefer it join all the strings in all the lists that would be okay too. So the syntax would be like this (on POSIX as an example):

On Tuesday, July 29, 2014 1:14 AM, Todd <toddrjen@gmail.com> wrote:
Currently, os.path.join joins strings specified in its arguments, with one string per argument.
On its own, that is not a problem. However, it is inconsistent with str.join, which accepts only a list of strings.
No, str.join accepts any iterable of strings—including a string, which is an iterable of single-character strings. Not that you often intentionally pass a string to it, but you do very often pass a generator expression or other iterator, so treating lists specially for os.path.join to make it work more like str.join would just increase confusion, not reduce it. Also, I don't know of anything else in Python that has special treatment for lists vs. other iterables. There are a few cases that have special treatment for _tuples_ (notably str.__mod__), but I don't think anyone wants to expand those, and I don't think it would make you happy here, either.

On 7/29/2014 4:05 AM, Todd wrote:
Currently, os.path.join joins strings specified in its arguments, with one string per argument.
One typically has 2 or possibly 3 path segements, never 1000.
I partly agree, but think about the actually use cases.
My suggestion is to allow os.path.join to accept a list of strings in addition to existing one string per argument.
os.path.join(*iterable) -- Terry Jan Reedy

On Tuesday, July 29, 2014 1:14 AM, Todd <toddrjen@gmail.com> wrote:
Currently, os.path.join joins strings specified in its arguments, with one string per argument.
On its own, that is not a problem. However, it is inconsistent with str.join, which accepts only a list of strings.
No, str.join accepts any iterable of strings—including a string, which is an iterable of single-character strings. Not that you often intentionally pass a string to it, but you do very often pass a generator expression or other iterator, so treating lists specially for os.path.join to make it work more like str.join would just increase confusion, not reduce it. Also, I don't know of anything else in Python that has special treatment for lists vs. other iterables. There are a few cases that have special treatment for _tuples_ (notably str.__mod__), but I don't think anyone wants to expand those, and I don't think it would make you happy here, either.

On 7/29/2014 4:05 AM, Todd wrote:
Currently, os.path.join joins strings specified in its arguments, with one string per argument.
One typically has 2 or possibly 3 path segements, never 1000.
I partly agree, but think about the actually use cases.
My suggestion is to allow os.path.join to accept a list of strings in addition to existing one string per argument.
os.path.join(*iterable) -- Terry Jan Reedy
participants (4)
-
Andrew Barnert
-
Jonas Wielicki
-
Terry Reedy
-
Todd