Is there a “breadth-first” search option available in os.walk() or equivalent Python function?
up vote
2
down vote
favorite
Example directory tree:
root
/|
/ |
/ |
A B C
/
/
D E
F
G
os.walk() will traverse this directory tree using the depth-first search algorithm. For example, os.walk() will process this tree in this order: root, A, B, D, C, E, F, G. os.walk() doesn't seem to provide an option for a breadth-first search. If this option were available, it would process this tree in this order instead: root, A, B, C, D, E, F, G. In my application, I need to do the reverse search. However, os.walk(tree, topdown = False) yields: A, D, B, G, F, E, C, root. On the contrary, the breadth-first search in reverse would yield: G, F, E, D, C, B, A, root.
I've had to create my own solution which is below:
def reversewalk(path):
dirlist =
for dirName, subdirList, fileList in os.walk(path, topdown=False):
depth = dirName.count(os.path.sep)
dirlist[os.path.abspath(dirName)] = (depth, dirName, subdirList, fileList)
return sorted(dirlist.items(), key = lambda x : x[1], reverse = True)
My question is: Is there a "breadth-first" search option available in os.walk() or equivalent Python function? Follow up question is: If not, is there a better solution than the one I've presented?
python
|
show 8 more comments
up vote
2
down vote
favorite
Example directory tree:
root
/|
/ |
/ |
A B C
/
/
D E
F
G
os.walk() will traverse this directory tree using the depth-first search algorithm. For example, os.walk() will process this tree in this order: root, A, B, D, C, E, F, G. os.walk() doesn't seem to provide an option for a breadth-first search. If this option were available, it would process this tree in this order instead: root, A, B, C, D, E, F, G. In my application, I need to do the reverse search. However, os.walk(tree, topdown = False) yields: A, D, B, G, F, E, C, root. On the contrary, the breadth-first search in reverse would yield: G, F, E, D, C, B, A, root.
I've had to create my own solution which is below:
def reversewalk(path):
dirlist =
for dirName, subdirList, fileList in os.walk(path, topdown=False):
depth = dirName.count(os.path.sep)
dirlist[os.path.abspath(dirName)] = (depth, dirName, subdirList, fileList)
return sorted(dirlist.items(), key = lambda x : x[1], reverse = True)
My question is: Is there a "breadth-first" search option available in os.walk() or equivalent Python function? Follow up question is: If not, is there a better solution than the one I've presented?
python
1
If your function works, this is more appropriate for code review.
– user3483203
Apr 4 at 14:57
I think you want to play withtopdown=False
option.
– Jean-François Fabre
Apr 4 at 14:57
As I said in the posting above, the topdown=False option does not work the way I need it to work.
– Johnas Cukier
Apr 4 at 14:58
@Jean-FrançoisFabre I believetopdown=False
is still a variant of DFS
– user3483203
Apr 4 at 14:59
@JohnasCukier code.activestate.com/recipes/511456-breadth-first-file-iterator
– user3483203
Apr 4 at 14:59
|
show 8 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Example directory tree:
root
/|
/ |
/ |
A B C
/
/
D E
F
G
os.walk() will traverse this directory tree using the depth-first search algorithm. For example, os.walk() will process this tree in this order: root, A, B, D, C, E, F, G. os.walk() doesn't seem to provide an option for a breadth-first search. If this option were available, it would process this tree in this order instead: root, A, B, C, D, E, F, G. In my application, I need to do the reverse search. However, os.walk(tree, topdown = False) yields: A, D, B, G, F, E, C, root. On the contrary, the breadth-first search in reverse would yield: G, F, E, D, C, B, A, root.
I've had to create my own solution which is below:
def reversewalk(path):
dirlist =
for dirName, subdirList, fileList in os.walk(path, topdown=False):
depth = dirName.count(os.path.sep)
dirlist[os.path.abspath(dirName)] = (depth, dirName, subdirList, fileList)
return sorted(dirlist.items(), key = lambda x : x[1], reverse = True)
My question is: Is there a "breadth-first" search option available in os.walk() or equivalent Python function? Follow up question is: If not, is there a better solution than the one I've presented?
python
Example directory tree:
root
/|
/ |
/ |
A B C
/
/
D E
F
G
os.walk() will traverse this directory tree using the depth-first search algorithm. For example, os.walk() will process this tree in this order: root, A, B, D, C, E, F, G. os.walk() doesn't seem to provide an option for a breadth-first search. If this option were available, it would process this tree in this order instead: root, A, B, C, D, E, F, G. In my application, I need to do the reverse search. However, os.walk(tree, topdown = False) yields: A, D, B, G, F, E, C, root. On the contrary, the breadth-first search in reverse would yield: G, F, E, D, C, B, A, root.
I've had to create my own solution which is below:
def reversewalk(path):
dirlist =
for dirName, subdirList, fileList in os.walk(path, topdown=False):
depth = dirName.count(os.path.sep)
dirlist[os.path.abspath(dirName)] = (depth, dirName, subdirList, fileList)
return sorted(dirlist.items(), key = lambda x : x[1], reverse = True)
My question is: Is there a "breadth-first" search option available in os.walk() or equivalent Python function? Follow up question is: If not, is there a better solution than the one I've presented?
python
python
edited Apr 4 at 15:19
asked Apr 4 at 14:55
Johnas Cukier
3791310
3791310
1
If your function works, this is more appropriate for code review.
– user3483203
Apr 4 at 14:57
I think you want to play withtopdown=False
option.
– Jean-François Fabre
Apr 4 at 14:57
As I said in the posting above, the topdown=False option does not work the way I need it to work.
– Johnas Cukier
Apr 4 at 14:58
@Jean-FrançoisFabre I believetopdown=False
is still a variant of DFS
– user3483203
Apr 4 at 14:59
@JohnasCukier code.activestate.com/recipes/511456-breadth-first-file-iterator
– user3483203
Apr 4 at 14:59
|
show 8 more comments
1
If your function works, this is more appropriate for code review.
– user3483203
Apr 4 at 14:57
I think you want to play withtopdown=False
option.
– Jean-François Fabre
Apr 4 at 14:57
As I said in the posting above, the topdown=False option does not work the way I need it to work.
– Johnas Cukier
Apr 4 at 14:58
@Jean-FrançoisFabre I believetopdown=False
is still a variant of DFS
– user3483203
Apr 4 at 14:59
@JohnasCukier code.activestate.com/recipes/511456-breadth-first-file-iterator
– user3483203
Apr 4 at 14:59
1
1
If your function works, this is more appropriate for code review.
– user3483203
Apr 4 at 14:57
If your function works, this is more appropriate for code review.
– user3483203
Apr 4 at 14:57
I think you want to play with
topdown=False
option.– Jean-François Fabre
Apr 4 at 14:57
I think you want to play with
topdown=False
option.– Jean-François Fabre
Apr 4 at 14:57
As I said in the posting above, the topdown=False option does not work the way I need it to work.
– Johnas Cukier
Apr 4 at 14:58
As I said in the posting above, the topdown=False option does not work the way I need it to work.
– Johnas Cukier
Apr 4 at 14:58
@Jean-FrançoisFabre I believe
topdown=False
is still a variant of DFS– user3483203
Apr 4 at 14:59
@Jean-FrançoisFabre I believe
topdown=False
is still a variant of DFS– user3483203
Apr 4 at 14:59
@JohnasCukier code.activestate.com/recipes/511456-breadth-first-file-iterator
– user3483203
Apr 4 at 14:59
@JohnasCukier code.activestate.com/recipes/511456-breadth-first-file-iterator
– user3483203
Apr 4 at 14:59
|
show 8 more comments
2 Answers
2
active
oldest
votes
up vote
0
down vote
The following code is from an ActiveState article I read:
#!/usr/bin/env python
import os
# -------------------------------------------
def breadthFirstFileScan( root ) :
dirs = [root]
# while we has dirs to scan
while len(dirs) :
nextDirs =
for parent in dirs :
# scan each dir
for f in os.listdir( parent ) :
# if there is a dir, then save for next ittr
# if it is a file then yield it (we'll return later)
ff = os.path.join( parent, f )
if os.path.isdir( ff ) :
nextDirs.append( ff )
else :
yield ff
# once we've done all the current dirs then
# we set up the next itter as the child dirs
# from the current itter.
dirs = nextDirs
# -------------------------------------------
# an example func that just outputs the files.
def walkbf( path ) :
for f in breadthFirstFileScan( path ) :
print f
# ============================================
# as a demo we'll just start from where we
# were called from.
walkbf( os.getcwd() )
add a comment |
up vote
0
down vote
The following is just a bit more terse and more closely mimics os.walk()'s functionality. (It's interesting this works because of Python's implicit handling of lexicographic sorting of tuples.)
def rbf_walk(path):
dirlist = ((dirpath.count(os.path.sep), dirpath, dirnames, filenames) for
dirpath, dirnames, filenames in os.walk(path, topdown = False))
for entry in sorted(dirlist, reverse = True):
yield entry[1:]
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
The following code is from an ActiveState article I read:
#!/usr/bin/env python
import os
# -------------------------------------------
def breadthFirstFileScan( root ) :
dirs = [root]
# while we has dirs to scan
while len(dirs) :
nextDirs =
for parent in dirs :
# scan each dir
for f in os.listdir( parent ) :
# if there is a dir, then save for next ittr
# if it is a file then yield it (we'll return later)
ff = os.path.join( parent, f )
if os.path.isdir( ff ) :
nextDirs.append( ff )
else :
yield ff
# once we've done all the current dirs then
# we set up the next itter as the child dirs
# from the current itter.
dirs = nextDirs
# -------------------------------------------
# an example func that just outputs the files.
def walkbf( path ) :
for f in breadthFirstFileScan( path ) :
print f
# ============================================
# as a demo we'll just start from where we
# were called from.
walkbf( os.getcwd() )
add a comment |
up vote
0
down vote
The following code is from an ActiveState article I read:
#!/usr/bin/env python
import os
# -------------------------------------------
def breadthFirstFileScan( root ) :
dirs = [root]
# while we has dirs to scan
while len(dirs) :
nextDirs =
for parent in dirs :
# scan each dir
for f in os.listdir( parent ) :
# if there is a dir, then save for next ittr
# if it is a file then yield it (we'll return later)
ff = os.path.join( parent, f )
if os.path.isdir( ff ) :
nextDirs.append( ff )
else :
yield ff
# once we've done all the current dirs then
# we set up the next itter as the child dirs
# from the current itter.
dirs = nextDirs
# -------------------------------------------
# an example func that just outputs the files.
def walkbf( path ) :
for f in breadthFirstFileScan( path ) :
print f
# ============================================
# as a demo we'll just start from where we
# were called from.
walkbf( os.getcwd() )
add a comment |
up vote
0
down vote
up vote
0
down vote
The following code is from an ActiveState article I read:
#!/usr/bin/env python
import os
# -------------------------------------------
def breadthFirstFileScan( root ) :
dirs = [root]
# while we has dirs to scan
while len(dirs) :
nextDirs =
for parent in dirs :
# scan each dir
for f in os.listdir( parent ) :
# if there is a dir, then save for next ittr
# if it is a file then yield it (we'll return later)
ff = os.path.join( parent, f )
if os.path.isdir( ff ) :
nextDirs.append( ff )
else :
yield ff
# once we've done all the current dirs then
# we set up the next itter as the child dirs
# from the current itter.
dirs = nextDirs
# -------------------------------------------
# an example func that just outputs the files.
def walkbf( path ) :
for f in breadthFirstFileScan( path ) :
print f
# ============================================
# as a demo we'll just start from where we
# were called from.
walkbf( os.getcwd() )
The following code is from an ActiveState article I read:
#!/usr/bin/env python
import os
# -------------------------------------------
def breadthFirstFileScan( root ) :
dirs = [root]
# while we has dirs to scan
while len(dirs) :
nextDirs =
for parent in dirs :
# scan each dir
for f in os.listdir( parent ) :
# if there is a dir, then save for next ittr
# if it is a file then yield it (we'll return later)
ff = os.path.join( parent, f )
if os.path.isdir( ff ) :
nextDirs.append( ff )
else :
yield ff
# once we've done all the current dirs then
# we set up the next itter as the child dirs
# from the current itter.
dirs = nextDirs
# -------------------------------------------
# an example func that just outputs the files.
def walkbf( path ) :
for f in breadthFirstFileScan( path ) :
print f
# ============================================
# as a demo we'll just start from where we
# were called from.
walkbf( os.getcwd() )
answered May 9 at 19:01
Watty62
212116
212116
add a comment |
add a comment |
up vote
0
down vote
The following is just a bit more terse and more closely mimics os.walk()'s functionality. (It's interesting this works because of Python's implicit handling of lexicographic sorting of tuples.)
def rbf_walk(path):
dirlist = ((dirpath.count(os.path.sep), dirpath, dirnames, filenames) for
dirpath, dirnames, filenames in os.walk(path, topdown = False))
for entry in sorted(dirlist, reverse = True):
yield entry[1:]
add a comment |
up vote
0
down vote
The following is just a bit more terse and more closely mimics os.walk()'s functionality. (It's interesting this works because of Python's implicit handling of lexicographic sorting of tuples.)
def rbf_walk(path):
dirlist = ((dirpath.count(os.path.sep), dirpath, dirnames, filenames) for
dirpath, dirnames, filenames in os.walk(path, topdown = False))
for entry in sorted(dirlist, reverse = True):
yield entry[1:]
add a comment |
up vote
0
down vote
up vote
0
down vote
The following is just a bit more terse and more closely mimics os.walk()'s functionality. (It's interesting this works because of Python's implicit handling of lexicographic sorting of tuples.)
def rbf_walk(path):
dirlist = ((dirpath.count(os.path.sep), dirpath, dirnames, filenames) for
dirpath, dirnames, filenames in os.walk(path, topdown = False))
for entry in sorted(dirlist, reverse = True):
yield entry[1:]
The following is just a bit more terse and more closely mimics os.walk()'s functionality. (It's interesting this works because of Python's implicit handling of lexicographic sorting of tuples.)
def rbf_walk(path):
dirlist = ((dirpath.count(os.path.sep), dirpath, dirnames, filenames) for
dirpath, dirnames, filenames in os.walk(path, topdown = False))
for entry in sorted(dirlist, reverse = True):
yield entry[1:]
answered Nov 11 at 16:06
ppw0
3616
3616
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49654234%2fis-there-a-breadth-first-search-option-available-in-os-walk-or-equivalent-py%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
If your function works, this is more appropriate for code review.
– user3483203
Apr 4 at 14:57
I think you want to play with
topdown=False
option.– Jean-François Fabre
Apr 4 at 14:57
As I said in the posting above, the topdown=False option does not work the way I need it to work.
– Johnas Cukier
Apr 4 at 14:58
@Jean-FrançoisFabre I believe
topdown=False
is still a variant of DFS– user3483203
Apr 4 at 14:59
@JohnasCukier code.activestate.com/recipes/511456-breadth-first-file-iterator
– user3483203
Apr 4 at 14:59