Get all subdirs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Florian Lindner

    #1

    Get all subdirs

    Hello,
    how can I get all subdirectories of a given directory. os.listdir(dir)
    doesn't differentiate between directories and files, os.walk seems to me a
    bit overkill since it also descends in the subdirs.
    Thx,
    Florian
  • Leif K-Brooks

    #2
    Re: Get all subdirs

    Florian Lindner wrote:[color=blue]
    > how can I get all subdirectories of a given directory[/color]
    [color=blue][color=green][color=darkred]
    >>> import os
    >>> def subdirs(dir):[/color][/color][/color]
    .... return [filename for filename in os.listdir(dir)
    .... if os.path.isdir(o s.path.join(dir , filename))]
    ....[color=blue][color=green][color=darkred]
    >>> subdirs('.')[/color][/color][/color]
    ['foo', 'bar', 'baz']

    Comment

    • Peter Kleiweg

      #3
      Re: Get all subdirs

      Florian Lindner schreef:
      [color=blue]
      > Hello,
      > how can I get all subdirectories of a given directory. os.listdir(dir)
      > doesn't differentiate between directories and files, os.walk seems to me a
      > bit overkill since it also descends in the subdirs.[/color]

      #!/usr/bin/env python

      import os

      dir = '.' # '.' is current directory

      for i in os.listdir(dir) :
      if os.path.isdir(o s.path.join(dir , i)):
      print i


      Bby the way, on Linux, I noticed that the directories '.' and
      '..' are not returned by os.listdir


      --
      Peter Kleiweg L:NL,af,da,de,e n,ia,nds,no,sv, (fr,it) S:NL,de,en,(da, ia)
      info: http://www.let.rug.nl/~kleiweg/ls.html

      Comment

      • George Yoshida

        #4
        Re: Get all subdirs

        Florian Lindner wrote:[color=blue]
        > Hello,
        > how can I get all subdirectories of a given directory. os.listdir(dir)
        > doesn't differentiate between directories and files, os.walk seems to me a
        > bit overkill since it also descends in the subdirs.
        > Thx,
        > Florian[/color]

        What about testing return values of os.listdir with os.path.isdir?
        e.g.,

        filter(os.path. isdir, os.listdir(dir) )

        or if you don't like high-order functions,

        [filename for filename in os.listdir(dir) if os.path.isdir(f ilename)]

        --
        George

        Comment

        • Stephen Boulet

          #5
          Re: Get all subdirs

          Florian Lindner wrote:[color=blue]
          > Hello,
          > how can I get all subdirectories of a given directory. os.listdir(dir)
          > doesn't differentiate between directories and files, os.walk seems to me a
          > bit overkill since it also descends in the subdirs.
          > Thx,
          > Florian[/color]

          I'm a fan of the path module:

          from path import path

          p = path("my_direct ory")
          l = [i.name for i in p.dirs()]

          Stephen

          Comment

          • Tim Peters

            #6
            Re: Get all subdirs

            [Florian Lindner][color=blue]
            > how can I get all subdirectories of a given directory. os.listdir(dir)
            > doesn't differentiate between directories and files, os.walk seems to me a
            > bit overkill since it also descends in the subdirs.[/color]

            os.walk() is a generator -- it doesn't descend into anything unless
            you resume it. That's the usual case, but you don't need to resume
            it.

            def subdirs(dir):
            "Return list of the subdirectories of dir."
            for root, dirs, files in os.walk(dir):
            return dirs

            works fine, or, perhaps more obscurely,

            def subdirs(dir):
            "Return list of the subdirectories of dir."
            return os.walk(dir).ne xt()[1]

            Comment

            Working...