Exclude Directories from os.walk

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

    #1

    Exclude Directories from os.walk

    Hello,

    How can one exclude a directory (and all its subdirectories) when
    running os.walk()?

    Thanks,

    Doug
  • Scott David Daniels

    #2
    Re: Exclude Directories from os.walk

    D wrote:
    Hello,
    >
    How can one exclude a directory (and all its subdirectories) when
    running os.walk()?
    >
    Thanks,
    >
    Doug
    for base, dirs, files in os.walk('wherev er'):
    if 'RCS' in dirs:
    dirs.remove('RC S')

    As described in the os.walk docs.


    --Scott David Daniels
    Scott.Daniels@A cm.Org

    Comment

    • Tim Golden

      #3
      Re: Exclude Directories from os.walk

      D wrote:
      Hello,
      >
      How can one exclude a directory (and all its subdirectories) when
      running os.walk()?
      Just remove it from the dirnames yielded:

      <code>
      import os

      for dirpath, dirnames, filenames in os.walk ("c:/temp"):
      print dirpath
      if "archive" in dirnames:
      dirnames.remove ("archive")

      </code>

      TJG

      Comment

      • D

        #4
        Re: Exclude Directories from os.walk

        Ok, my brain's apparently not working right today.. what I'd like to
        do is allow the user to specify a directory to exclude (ex- "C:\temp
        \test") - then, when os.walk gets to "C:\temp\te st", it excludes that
        directory and all its subdirectories (so, "C:\temp\mytest \test" should
        still be recursed). Isn't what's posted above keying just upon the
        final subdirectory (i.e. "test") instead of the full path as a whole?

        Comment

        • Orestis Markou

          #5
          Re: Exclude Directories from os.walk

          You then have to also check the base:

          for d in dirs[:]:
          if os.path.join(ba se, d) == EXCLUDED_DIR
          dirs.remove(d)

          or

          if base == EXCLUDED_DIR
          while dirs: dirs.pop()
          continue

          WARNING: untest code
          - Show quoted text -

          On Tue, Oct 21, 2008 at 6:13 PM, D <dmcclouds@gmai l.comwrote:
          Ok, my brain's apparently not working right today.. what I'd like to
          do is allow the user to specify a directory to exclude (ex- "C:\temp
          \test") - then, when os.walk gets to "C:\temp\te st", it excludes that
          directory and all its subdirectories (so, "C:\temp\mytest \test" should
          still be recursed). Isn't what's posted above keying just upon the
          final subdirectory (i.e. "test") instead of the full path as a whole?
          >
          --

          >


          --
          orestis@orestis .gr

          Comment

          • D

            #6
            Re: Exclude Directories from os.walk

            On Oct 21, 1:46 pm, "Orestis Markou" <ores...@oresti s.grwrote:
            You then have to also check the base:
            >
            for d in dirs[:]:
             if os.path.join(ba se, d) == EXCLUDED_DIR
               dirs.remove(d)
            >
            or
            >
            if base == EXCLUDED_DIR
              while dirs: dirs.pop()
            continue
            >
            WARNING: untest code
            - Show quoted text -
            >
            On Tue, Oct 21, 2008 at 6:13 PM, D <dmcclo...@gmai l.comwrote:
            Ok, my brain's apparently not working right today.. what I'd like to
            do is allow the user to specify a directory to exclude (ex- "C:\temp
            \test") - then, when os.walk gets to "C:\temp\te st", it excludes that
            directory and all its subdirectories (so, "C:\temp\mytest \test" should
            still be recursed).  Isn't what's posted above keying just upon the
            final subdirectory (i.e. "test") instead of the full path as a whole?
            >>
            --
            ores...@orestis .grhttp://orestis.gr
            Works like a charm - thanks to everyone for your help!

            Doug

            Comment

            • Scott David Daniels

              #7
              Re: Exclude Directories from os.walk

              D wrote:
              Ok, my brain's apparently not working right today.. what I'd like to
              do is allow the user to specify a directory to exclude (ex- "C:\temp
              \test") - then, when os.walk gets to "C:\temp\te st", it excludes that
              directory and all its subdirectories (so, "C:\temp\mytest \test" should
              still be recursed). Isn't what's posted above keying just upon the
              final subdirectory (i.e. "test") instead of the full path as a whole?
              >
              def canonical(path_ name):
              return os.path.normpat h(os.path.abspa th(path_name))

              forbidden = 'a/b/c'
              home, final = os.path.split(c anonical(forbid den))
              for base, dirs, files in os.walk('wherev er'):
              if final in dirs and home == canonical(base) :
              dirs.remove(fin al)


              --Scott David Daniels
              Scott.Daniels@A cm.Org

              Comment

              Working...