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?










share|improve this question



















  • 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














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?










share|improve this question



















  • 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












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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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












  • 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







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












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() )





share|improve this answer



























    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:]





    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      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

























      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() )





      share|improve this answer
























        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() )





        share|improve this answer






















          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() )





          share|improve this answer












          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() )






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 9 at 19:01









          Watty62

          212116




          212116






















              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:]





              share|improve this answer
























                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:]





                share|improve this answer






















                  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:]





                  share|improve this answer












                  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:]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 16:06









                  ppw0

                  3616




                  3616



























                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                      Museum of Modern and Contemporary Art of Trento and Rovereto