[Python-ideas] os.path.split(path, maxsplit=1)
anatoly techtonik
techtonik at gmail.com
Wed Nov 7 07:23:49 CET 2012
On Tue, Nov 6, 2012 at 1:22 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> On 05/11/12 23:52, Ned Batchelder wrote:
>
>> Anatoly, I appreciate the energy and dedication you've shown to the
>> Python community, but maybe you should spend a little more time on
>> each proposal? For example, the subject line here is a different (and
>> already taken) function name than the implementation, and has a
>> maxsplit argument that the implementation doesn't have.
It's the idea to add maxsplit argument to os.path.split(). If the idea
is good, it will be developed into actual proposal.
I've included prototype code, because in the past people complained
about the absence of source code. The name in prototype function is
different, because it uses os.path.split internally, which clashes.
Here is the working prototype. Attached is with test case from SO.
Note that it behaves differently on Windows, Python 3 because of the
regression http://bugs.python.org/issue16424
def pathsplit(pathstr, maxsplit=):
"""split relative path into list"""
path = [pathstr]
while True:
oldpath = path[:]
path[:1] = list(os.path.split(path[0]))
if path[0] == '':
path = path[1:]
elif path[1] == '':
path = path[:1] + path[2:]
if path == oldpath:
return path
if maxsplit is not None and len(path) > maxsplit:
return path
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pathsplit.py
Type: application/octet-stream
Size: 1144 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20121107/cdc131a3/attachment.obj>
More information about the Python-ideas
mailing list