Searching

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

    Searching

    Hi I was wondering if one of you could help me out

    I would like for this function to be able to find file names as well instead
    of printing out every file that ends with a .jpg or .gif extension
    but I don't know how to use reg expressions and yes I have been trying to
    figure them out for the past day and a half now

    def searchfiles(sel f):
    filenames = {'jpg': [], 'gif': []}
    path = 'S:/'
    for root, dirs, files in os.walk(path):
    for name in files:
    # This is the part that needs to be changed I just don't know
    how to do it
    if name.endswith(' .jpg'): # right here this should be what
    needs to be changed
    filenames['jpg'].append(os.path .join(root, name))
    self.listbox1.i nsert(END, name)
    elif name.endswith(' .gif'): #As well as this
    filenames['gif'].append(os.path .join(root, name))
    self.listbox1.i nsert(END, name)

    I should let you know that this is a function attached to a button that
    searches my harddrive for files it has an entry field to do the searching
    but it prints out every image file I want to limit it the name typed in eg
    if I type ocean every file with the name ocean appears



  • Scott David Daniels

    #2
    Re: Searching

    ADE wrote:
    [color=blue]
    > Hi I was wondering if one of you could help me out[/color]
    You are missing info on your system and python version.
    If you are running 2.3 or later:[color=blue]
    >...
    > for name in files:
    > name = name.lower() # if on windows
    > if name.endswith(' .jpg') and 'ocean' in name:
    > filenames['jpg'].append(os.path .join(root, name))
    > self.listbox1.i nsert(END, name)
    > elif name.endswith(' .gif') and 'ocean' in name:
    > filenames['gif'].append(os.path .join(root, name))
    > self.listbox1.i nsert(END, name)[/color]

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

    Comment

    • ADE

      #3
      Re: Searching

      Hi sorry about that I am running Python 2.3 and Windows XP

      "Scott David Daniels" <Scott.Daniels@ Acm.Org> wrote in message
      news:40280886$1 @nntp0.pdx.net. ..[color=blue]
      > ADE wrote:
      >[color=green]
      > > Hi I was wondering if one of you could help me out[/color]
      > You are missing info on your system and python version.
      > If you are running 2.3 or later:[color=green]
      > >...
      > > for name in files:
      > > name = name.lower() # if on windows
      > > if name.endswith(' .jpg') and 'ocean' in name:
      > > filenames['jpg'].append(os.path .join(root, name))
      > > self.listbox1.i nsert(END, name)
      > > elif name.endswith(' .gif') and 'ocean' in name:
      > > filenames['gif'].append(os.path .join(root, name))
      > > self.listbox1.i nsert(END, name)[/color]
      >
      > --
      > -Scott David Daniels
      > Scott.Daniels@A cm.Org[/color]


      Comment

      • Miki Tebeka

        #4
        Re: Searching

        Hello,
        [color=blue]
        > I should let you know that this is a function attached to a button that
        > searches my harddrive for files it has an entry field to do the searching
        > but it prints out every image file I want to limit it the name typed in eg
        > if I type ocean every file with the name ocean appears[/color]
        Have a look at the `glob' module.

        HTH.
        Miki

        Comment

        • Scott David Daniels

          #5
          Re: Searching

          Perhaps better (adds '.png' more easily, keeps original case):
          ...
          for name in files:
          lname = name.lower() # if on windows
          for ext, names in filenames.iteri tems():
          if lname.endswith( ext) and 'ocean' in name:
          names.append(os .path.join(root , name))
          self.listbox1.i nsert(END, name)
          break
          ...
          --
          -Scott David Daniels
          Scott.Daniels@A cm.Org

          Comment

          Working...