path stuff

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

    #1

    path stuff

    I am walking some directories looking for a certain filename pattern.
    This part works fine, but what if I want to exclude results from a
    certain directory being printed?

    eg

    d:\dir\mydir1\f ilename.txt <----------I want to
    see this one
    d:\dir\mydir2\a rchived\filenam e.txt <----------I don't want to
    see anything in the "archived" directory
    d:\dir\mydir2\f ilename.txt <----------Again, I do
    want to see this one

    I am having a bit of trouble figuring out how to use the path module
    to hack up the path to determine if I am in a subdir I care about. So
    either don show me the results from a certain directory or just plain
    skip a certain directory.

  • kyosohma@gmail.com

    #2
    Re: path stuff

    On May 9, 1:11 pm, fscked <fsckedag...@gm ail.comwrote:
    I am walking some directories looking for a certain filename pattern.
    This part works fine, but what if I want to exclude results from a
    certain directory being printed?
    >
    eg
    >
    d:\dir\mydir1\f ilename.txt <----------I want to
    see this one
    d:\dir\mydir2\a rchived\filenam e.txt <----------I don't want to
    see anything in the "archived" directory
    d:\dir\mydir2\f ilename.txt <----------Again, I do
    want to see this one
    >
    I am having a bit of trouble figuring out how to use the path module
    to hack up the path to determine if I am in a subdir I care about. So
    either don show me the results from a certain directory or just plain
    skip a certain directory.
    Hi,

    One way to do it would be to grab just the directory path like this:

    dirPath = os.path.dirname (path)

    and then use and if:

    if 'archived' in dirPath:
    # skip this directory

    That should get you closer to the answer anyway.

    Mike

    Comment

    • Gabriel Genellina

      #3
      Re: path stuff

      En Wed, 09 May 2007 15:11:06 -0300, fscked <fsckedagain@gm ail.com>
      escribió:
      I am walking some directories looking for a certain filename pattern.
      This part works fine, but what if I want to exclude results from a
      certain directory being printed?
      Using os.walk you can skip undesired directories entirely:

      for dirpath, dirnames, filenames in os.walk(startin g_dir):
      if "archived" in dirnames:
      dirnames.remove ("archived")
      # process filenames, typically:
      for filename in filenames:
      fullfn = os.path.join(di rpath, filename)
      ...

      --
      Gabriel Genellina

      Comment

      • fscked

        #4
        Re: path stuff

        On May 9, 7:02 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote:
        En Wed, 09 May 2007 15:11:06 -0300, fscked <fsckedag...@gm ail.com
        escribió:
        >
        I am walking some directories looking for a certain filename pattern.
        This part works fine, but what if I want to exclude results from a
        certain directory being printed?
        >
        Using os.walk you can skip undesired directories entirely:
        >
        for dirpath, dirnames, filenames in os.walk(startin g_dir):
        if "archived" in dirnames:
        dirnames.remove ("archived")
        # process filenames, typically:
        for filename in filenames:
        fullfn = os.path.join(di rpath, filename)
        ...
        >
        --
        Gabriel Genellina
        OK, this is on Winbloze and it keeps giving me "The directory name is
        invalid: u"blahblahbl ah" with double backslashies everywhere. I am
        currently trying to figure out how to make those go away. I shall
        check back in a bit.

        thanks for all the help so far. :)

        Comment

        • fscked

          #5
          Re: path stuff

          On May 10, 10:41 am, fscked <fsckedag...@gm ail.comwrote:
          On May 9, 7:02 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote:
          >
          >
          >
          >
          >
          En Wed, 09 May 2007 15:11:06 -0300, fscked <fsckedag...@gm ail.com
          escribió:
          >
          I am walking some directories looking for a certain filename pattern.
          This part works fine, but what if I want to exclude results from a
          certain directory being printed?
          >
          Using os.walk you can skip undesired directories entirely:
          >
          for dirpath, dirnames, filenames in os.walk(startin g_dir):
          if "archived" in dirnames:
          dirnames.remove ("archived")
          # process filenames, typically:
          for filename in filenames:
          fullfn = os.path.join(di rpath, filename)
          ...
          >
          --
          Gabriel Genellina
          >
          OK, this is on Winbloze and it keeps giving me "The directory name is
          invalid: u"blahblahbl ah" with double backslashies everywhere. I am
          currently trying to figure out how to make those go away. I shall
          check back in a bit.
          >
          thanks for all the help so far. :)- Hide quoted text -
          >
          - Show quoted text -
          ok, got the backslashies fixed, not I want it to print just a single
          line for each matching filename and dirpath, but it prints 3... hmm...

          Comment

          • fscked

            #6
            Re: path stuff

            On May 10, 12:45 pm, fscked <fsckedag...@gm ail.comwrote:
            On May 10, 10:41 am, fscked <fsckedag...@gm ail.comwrote:
            >
            >
            >
            >
            >
            On May 9, 7:02 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote:
            >
            En Wed, 09 May 2007 15:11:06 -0300, fscked <fsckedag...@gm ail.com
            escribió:
            >
            I am walking some directories looking for a certain filename pattern.
            This part works fine, but what if I want to exclude results from a
            certain directory being printed?
            >
            Using os.walk you can skip undesired directories entirely:
            >
            for dirpath, dirnames, filenames in os.walk(startin g_dir):
            if "archived" in dirnames:
            dirnames.remove ("archived")
            # process filenames, typically:
            for filename in filenames:
            fullfn = os.path.join(di rpath, filename)
            ...
            >
            --
            Gabriel Genellina
            >
            OK, this is on Winbloze and it keeps giving me "The directory name is
            invalid: u"blahblahbl ah" with double backslashies everywhere. I am
            currently trying to figure out how to make those go away. I shall
            check back in a bit.
            >
            thanks for all the help so far. :)- Hide quoted text -
            >
            - Show quoted text -
            >
            ok, got the backslashies fixed, not I want it to print just a single
            line for each matching filename and dirpath, but it prints 3... hmm...- Hide quoted text -
            >
            - Show quoted text -
            Nevermind, I am indentationally challenged. I was printing under the
            for dirpath, dirname, filename part and had to unindent uno time.

            It works as desired now, thanks!

            Comment

            • fscked

              #7
              Re: path stuff

              On May 10, 1:43 pm, fscked <fsckedag...@gm ail.comwrote:
              On May 10, 12:45 pm, fscked <fsckedag...@gm ail.comwrote:
              >
              >
              >
              >
              >
              On May 10, 10:41 am, fscked <fsckedag...@gm ail.comwrote:
              >
              On May 9, 7:02 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote:
              >
              En Wed, 09 May 2007 15:11:06 -0300, fscked <fsckedag...@gm ail.com
              escribió:
              >
              I am walking some directories looking for a certain filename pattern.
              This part works fine, but what if I want to exclude results from a
              certain directory being printed?
              >
              Using os.walk you can skip undesired directories entirely:
              >
              for dirpath, dirnames, filenames in os.walk(startin g_dir):
              if "archived" in dirnames:
              dirnames.remove ("archived")
              # process filenames, typically:
              for filename in filenames:
              fullfn = os.path.join(di rpath, filename)
              ...
              >
              --
              Gabriel Genellina
              >
              OK, this is on Winbloze and it keeps giving me "The directory name is
              invalid: u"blahblahbl ah" with double backslashies everywhere. I am
              currently trying to figure out how to make those go away. I shall
              check back in a bit.
              >
              thanks for all the help so far. :)- Hide quoted text -
              >
              - Show quoted text -
              >
              ok, got the backslashies fixed, not I want it to print just a single
              line for each matching filename and dirpath, but it prints 3... hmm...-Hide quoted text -
              >
              - Show quoted text -
              >
              Nevermind, I am indentationally challenged. I was printing under the
              for dirpath, dirname, filename part and had to unindent uno time.
              >
              It works as desired now, thanks!- Hide quoted text -
              >
              - Show quoted text -
              ok, I lied, it is still doing the archived folders. Here is the code:

              import os, sys
              from path import path

              myfile = open("boxids.tx t", "r", 0)
              for line in myfile:
              d = 'D:\\Dir\\' + path(line.strip ())
              for f in d.walkfiles('*C onfig*.xml'):
              for dirpath, dirnames, filenames in os.walk(d):
              if "Archived" in dirnames:
              dirnames.remove ("Archived") #skip this directory
              print f
              print 'Done'


              when it does the print f it still shows me the dirs i don't want to
              see.

              any more ideas?

              TIA

              Comment

              • Gabriel Genellina

                #8
                Re: path stuff

                En Thu, 10 May 2007 19:04:30 -0300, fscked <fsckedagain@gm ail.com>
                escribió:
                ok, I lied, it is still doing the archived folders. Here is the code:
                >
                import os, sys
                from path import path
                >
                myfile = open("boxids.tx t", "r", 0)
                for line in myfile:
                d = 'D:\\Dir\\' + path(line.strip ())
                for f in d.walkfiles('*C onfig*.xml'):
                for dirpath, dirnames, filenames in os.walk(d):
                if "Archived" in dirnames:
                dirnames.remove ("Archived") #skip this directory
                print f
                print 'Done'
                >
                >
                when it does the print f it still shows me the dirs i don't want to
                see.
                You are walking the directory structure *twice*, using two different
                methods at the same time. Also, there is no standard `path` module, and
                several implementations around, so it would be a good idea to tell us
                which one you use.
                If you want to omit a directory, and include just filenames matching a
                pattern:

                import os, sys, os.path, fnmatch

                def findinteresting files(root_dir) :
                for dirpath, dirnames, filenames in os.walk(root_di r):
                if "Archived" in dirnames:
                dirnames.remove ("Archived")
                for filename in fnmatch.filter( filenames, '*Config*.xml') :
                fullfn = os.path.join(di rpath, filename)
                print fullfn

                myfile = open("boxids.tx t", "r")
                for line in myfile:
                dirname = os.path.join('D :\\Dir\\', line.strip())
                findinteresting files(dirname):
                myfile.close()

                --
                Gabriel Genellina

                Comment

                • fscked

                  #9
                  Re: path stuff

                  On May 10, 6:08 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
                  wrote:
                  En Thu, 10 May 2007 19:04:30 -0300, fscked <fsckedag...@gm ail.com
                  escribió:
                  >
                  >
                  >
                  >
                  >
                  ok, I lied, it is still doing the archived folders. Here is the code:
                  >
                  import os, sys
                  from path import path
                  >
                  myfile = open("boxids.tx t", "r", 0)
                  for line in myfile:
                  d = 'D:\\Dir\\' + path(line.strip ())
                  for f in d.walkfiles('*C onfig*.xml'):
                  for dirpath, dirnames, filenames in os.walk(d):
                  if "Archived" in dirnames:
                  dirnames.remove ("Archived") #skip this directory
                  print f
                  print 'Done'
                  >
                  when it does the print f it still shows me the dirs i don't want to
                  see.
                  >
                  You are walking the directory structure *twice*, using two different
                  methods at the same time. Also, there is no standard `path` module, and
                  several implementations around, so it would be a good idea to tell us
                  which one you use.
                  If you want to omit a directory, and include just filenames matching a
                  pattern:
                  >
                  import os, sys, os.path, fnmatch
                  >
                  def findinteresting files(root_dir) :
                  for dirpath, dirnames, filenames in os.walk(root_di r):
                  if "Archived" in dirnames:
                  dirnames.remove ("Archived")
                  for filename in fnmatch.filter( filenames, '*Config*.xml') :
                  fullfn = os.path.join(di rpath, filename)
                  print fullfn
                  >
                  myfile = open("boxids.tx t", "r")
                  for line in myfile:
                  dirname = os.path.join('D :\\Dir\\', line.strip())
                  findinteresting files(dirname):
                  myfile.close()
                  >
                  --
                  Gabriel Genellina- Hide quoted text -
                  >
                  - Show quoted text -
                  Should this code work? I get a syntax error and cannot figure out why.

                  Comment

                  • Gabriel Genellina

                    #10
                    Re: path stuff

                    En Fri, 11 May 2007 13:25:55 -0300, fscked <fsckedagain@gm ail.com>
                    escribió:
                    >import os, sys, os.path, fnmatch
                    >>
                    >def findinteresting files(root_dir) :
                    > for dirpath, dirnames, filenames in os.walk(root_di r):
                    > if "Archived" in dirnames:
                    > dirnames.remove ("Archived")
                    > for filename in fnmatch.filter( filenames, '*Config*.xml') :
                    > fullfn = os.path.join(di rpath, filename)
                    > print fullfn
                    >>
                    >myfile = open("boxids.tx t", "r")
                    >for line in myfile:
                    > dirname = os.path.join('D :\\Dir\\', line.strip())
                    > findinteresting files(dirname):
                    >myfile.close ()
                    Should this code work? I get a syntax error and cannot figure out why.
                    Sorry, remove the spurious : after findinteresting files(dirname) and it
                    should work fine.

                    --
                    Gabriel Genellina

                    Comment

                    • BartlebyScrivener

                      #11
                      Re: path stuff

                      On May 9, 1:11 pm, fscked <fsckedag...@gm ail.comwrote:
                      I am walking some directories looking for a certain filename pattern.
                      This part works fine, but what if I want to exclude results from a
                      certain directory being printed?
                      You might find this thread helpful

                      http://tinyurl.com/2guk3l

                      Note how the backup dirs are excluded.

                      Highly recommend Python Cookbook, too.

                      rd

                      Comment

                      Working...