Discussion:
Accept list in os.path.join
Todd
2014-07-29 08:05:10 UTC
Permalink
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.
os.path.join('test1', 'test2', 'test3') # current syntax
'test1/test2/test3'
os.path.join(['test1', 'test2', 'test3']) # new syntax
'test1/test2/test3'
os.path.join(['test1', 'test2'], 'test3')
Exception
os.path.join(['test1'], 'test2', 'test3')
Exception
os.path.join(['test1', 'test2'], ['test3'])
Exception
Andrew Barnert
2014-07-29 09:12:28 UTC
Permalink
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.
Terry Reedy
2014-07-29 09:30:25 UTC
Permalink
Received: from localhost (HELO mail.python.org) (127.0.0.1)
by albatross.python.org with SMTP; 29 Jul 2014 11:30:48 +0200
Received: from plane.gmane.org (unknown [80.91.229.3])
(using TLSv1 with cipher AES256-SHA (256/256 bits))
(No client certificate requested)
by mail.python.org (Postfix) with ESMTPS
for <python-ideas-+ZN9ApsXKcEdnm+***@public.gmane.org>; Tue, 29 Jul 2014 11:30:48 +0200 (CEST)
Received: from list by plane.gmane.org with local (Exim 4.69)
(envelope-from <gcpi-python-***@m.gmane.org>) id 1XC3k7-0004Km-JZ
for python-ideas-+ZN9ApsXKcEdnm+***@public.gmane.org; Tue, 29 Jul 2014 11:30:47 +0200
Received: from pool-71-175-90-87.phlapa.fios.verizon.net ([71.175.90.87])
by main.gmane.org with esmtp (Gmexim 0.1 (Debian))
id 1AlnuQ-0007hv-00
for <python-ideas-+ZN9ApsXKcEdnm+***@public.gmane.org>; Tue, 29 Jul 2014 11:30:47 +0200
Received: from tjreedy by pool-71-175-90-87.phlapa.fios.verizon.net with local
(Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00
for <python-ideas-+ZN9ApsXKcEdnm+***@public.gmane.org>; Tue, 29 Jul 2014 11:30:47 +0200
X-Injected-Via-Gmane: http://gmane.org/
Lines: 21
X-Complaints-To: usenet-***@public.gmane.org
X-Gmane-NNTP-Posting-Host: pool-71-175-90-87.phlapa.fios.verizon.net
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64;
rv:24.0) Gecko/20100101 Thunderbird/24.6.0
In-Reply-To: <CAFpSVp+oDXdfbwwugxSZS7F8nRhMfse=4=X4TC2z8D_yiS3oVQ-JsoAwUIsXosN+***@public.gmane.org>
X-BeenThere: python-ideas-+ZN9ApsXKcEdnm+***@public.gmane.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: Discussions of speculative Python language ideas
<python-ideas.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-ideas>,
<mailto:python-ideas-request-+ZN9ApsXKcEdnm+***@public.gmane.org?subject=unsubscribe>
List-Archive: <http://mail.python.org/pipermail/python-ideas/>
List-Post: <mailto:python-ideas-+ZN9ApsXKcEdnm+***@public.gmane.org>
List-Help: <mailto:python-ideas-request-+ZN9ApsXKcEdnm+***@public.gmane.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-ideas>,
<mailto:python-ideas-request-+ZN9ApsXKcEdnm+***@public.gmane.org?subject=subscribe>
Errors-To: python-ideas-bounces+gcpi-python-ideas=m.gmane.org-+ZN9ApsXKcEdnm+***@public.gmane.org
Sender: "Python-ideas"
<python-ideas-bounces+gcpi-python-ideas=m.gmane.org-+ZN9ApsXKcEdnm+***@public.gmane.org>
Archived-At: <http://permalink.gmane.org/gmane.comp.python.ideas/28476>
Post by Todd
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.
Post by Todd
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.
I partly agree, but think about the actually use cases.
Post by Todd
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
Jonas Wielicki
2014-07-29 11:37:54 UTC
Permalink
Post by Todd
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.
From the implementation point of view, I have yet to see a duck-typing
way to distinguish a list (or any other iterable) of strings from a string.

regards,
jwi

Continue reading on narkive:
Loading...