Allow iterable argument to os.walk()

Given the push towards iterators in 3.0, is anyone in support of allowing an iterable for the "top" argument in os.walk? It seems like it would be common to look in more than one directory at once. - John

On Sun, Oct 30, 2011 at 8:47 AM, John O'Connor <jxo6948@rit.edu> wrote:
No, because it means you end up having to special case strings so they're treated atomically. We already do that in a few string-specific APIs (e.g. startswith()/endswith()), but it's still not a particularly nice pattern. If people want to walk multiple directories, 3.3 will make that pretty easy: def walk_dirs(dirs): for dir in dirs: yield from os.walk(dir) Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

It would be a special case, but I don't understand why that is a bad thing or why it is appropriate to do in some places but not others. Perhaps I am missing something. In this case, I think conceptually it makes sense to perform the "walk" on an iterable and also that one would expect strings to be treated atomically. I am well aware that not every 2-3 line recipe needs to be a new function but having more routines operate on iterables (where it makes sense) seems like a good idea. As an aside, I question the aesthetics of yet another for loop for the following. At least python has a nice for loop construct :) for dir in dirs: for root, dirs, files in os.walk(dir): for name in files: .... for name in dirs: ....

On Sun, Oct 30, 2011 at 1:51 PM, John O'Connor <jxo6948@rit.edu> wrote:
Maybe you forgot what the Zen of Python says about special cases. :-) "Special cases aren't special enough to break the rules."
It is often a good idea when the routine currently takes a specific iterable type (e.g. list). It is rarely a good idea to have a function that acts either on something of type X or a list of things of type X.
Whatever. :) -- --Guido van Rossum (python.org/~guido)

On Sun, Oct 30, 2011 at 8:47 AM, John O'Connor <jxo6948@rit.edu> wrote:
No, because it means you end up having to special case strings so they're treated atomically. We already do that in a few string-specific APIs (e.g. startswith()/endswith()), but it's still not a particularly nice pattern. If people want to walk multiple directories, 3.3 will make that pretty easy: def walk_dirs(dirs): for dir in dirs: yield from os.walk(dir) Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

It would be a special case, but I don't understand why that is a bad thing or why it is appropriate to do in some places but not others. Perhaps I am missing something. In this case, I think conceptually it makes sense to perform the "walk" on an iterable and also that one would expect strings to be treated atomically. I am well aware that not every 2-3 line recipe needs to be a new function but having more routines operate on iterables (where it makes sense) seems like a good idea. As an aside, I question the aesthetics of yet another for loop for the following. At least python has a nice for loop construct :) for dir in dirs: for root, dirs, files in os.walk(dir): for name in files: .... for name in dirs: ....

On Sun, Oct 30, 2011 at 1:51 PM, John O'Connor <jxo6948@rit.edu> wrote:
Maybe you forgot what the Zen of Python says about special cases. :-) "Special cases aren't special enough to break the rules."
It is often a good idea when the routine currently takes a specific iterable type (e.g. list). It is rarely a good idea to have a function that acts either on something of type X or a list of things of type X.
Whatever. :) -- --Guido van Rossum (python.org/~guido)
participants (4)
-
Guido van Rossum
-
John O'Connor
-
Nick Coghlan
-
Sven Marnach