os.path.split(path, maxsplit=1)

Why? Because it is critical when comparing paths and non-trivial. http://stackoverflow.com/questions/4579908/cross-platform-splitting-of-path-... http://stackoverflow.com/questions/3167154/how-to-split-a-dos-path-into-its-... http://www.gossamer-threads.com/lists/python/dev/654410 -- anatoly t.

Implementation. pathsplit('asd/asd\\asd\sad') == '['asd', 'asd', 'asd', 'sad'] def pathsplit(pathstr): """split relative path into list""" path = list(os.path.split(pathstr)) while '' not in path[:2]: path[:1] = list(os.path.split(path[0])) if path[0] == '': return path[1:] return path[:1] + path[2:] -- anatoly t. On Mon, Nov 5, 2012 at 9:48 AM, anatoly techtonik <techtonik@gmail.com> 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. Get everything the way you want it, and then propose it. --Ned. On 11/5/2012 7:41 AM, anatoly techtonik wrote:

On 05/11/12 23:52, Ned Batchelder wrote:
+1 Also consider publishing it as a recipe on ActiveState, where many people will view it, use it, and offer feedback. This has many benefits: * You will gauge community interest; * Many eyeballs make bugs shallow; * You are providing a useful recipe that others can use, even if it doesn't get included in the std lib. Some of the most useful parts of the std lib, like namedtuple, started life on ActiveState. http://code.activestate.com/recipes/langs/python/ -- Steven

On Tue, Nov 6, 2012 at 1:22 AM, Steven D'Aprano <steve@pearwood.info> wrote:
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

Implementation. pathsplit('asd/asd\\asd\sad') == '['asd', 'asd', 'asd', 'sad'] def pathsplit(pathstr): """split relative path into list""" path = list(os.path.split(pathstr)) while '' not in path[:2]: path[:1] = list(os.path.split(path[0])) if path[0] == '': return path[1:] return path[:1] + path[2:] -- anatoly t. On Mon, Nov 5, 2012 at 9:48 AM, anatoly techtonik <techtonik@gmail.com> 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. Get everything the way you want it, and then propose it. --Ned. On 11/5/2012 7:41 AM, anatoly techtonik wrote:

On 05/11/12 23:52, Ned Batchelder wrote:
+1 Also consider publishing it as a recipe on ActiveState, where many people will view it, use it, and offer feedback. This has many benefits: * You will gauge community interest; * Many eyeballs make bugs shallow; * You are providing a useful recipe that others can use, even if it doesn't get included in the std lib. Some of the most useful parts of the std lib, like namedtuple, started life on ActiveState. http://code.activestate.com/recipes/langs/python/ -- Steven

On Tue, Nov 6, 2012 at 1:22 AM, Steven D'Aprano <steve@pearwood.info> wrote:
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
participants (3)
-
anatoly techtonik
-
Ned Batchelder
-
Steven D'Aprano