#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    72
    Rep Power
    12

    Problem searching for files


    Hi I would like to know if there is an easy way for me to specify a few different file types eg .jpg .gif and be able to enter into a field a name and search for a file that has those extensions

    eg if I type the name sunsets I want to be able to find every file with the name sunsets or sun or sets

    I tried using OS.WALK but I was unable to figure it out from the documentation

    I am using python 2.3

    Any help is always appreciated

    Cheers

    Maboroshi
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Code:
    import os
    
    filenames = {'jpg': [], 'gif': []}
    
    path = '/home/foo'
    for root, dirs, files in os.walk(path):
        for name in files:
            if name.endswith('.jpg'):
                filenames['jpg'].append(os.path.join(root, name))
            elif name.endswith('.gif'):
                filenames['gif'].append(os.path.join(root, name))
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    72
    Rep Power
    12

    Hey Thank You


    Hey Thank you very much

    I have another question however I was wondering how I could limit the search to the contents of the search name it seems to print out every or almost every image with a gif or jpg extension

    Like if I type beach it should only find the images with a beach name with a gif or jpg extension

    Cheers

    and thank you
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    um, just replace "name.endswith('.jpg')" with whatever string checking you want
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    72
    Rep Power
    12

    Maybe a bit more info


    Hi and thank you again

    I should explain myself better

    I have a Tkinter GUI with an entry widget and a button widget and other widgets

    I would like to be able to enter information into my entry widget and search for it using the function attached to my button. Which is the one you have been helping me out with

    if I take out the .jpg or .gif in the name.endswith it will have everything appear which is not what I want

    I would like to do a search and find all the image files ending with a .jpg .gif or whatever using the name that was entered into the entry field in my gui

    So I guess my problem now is how do I use the entry fields value to do a search
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    No, don't take it out, replace it with the right stuff. The code wasn't meant to be used exactly, it was meant to teach you the concepts. If you don't understand what the code does, it does you no good.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    72
    Rep Power
    12

    Well I will be honest


    Yes I do not entirely understand the code

    but I will explain what I understand so far

    filenames = {'jpg': [], 'gif': []} this I assume is the extension to look for in the os

    path this I assume is the path to the directory to search in

    for root, dirs, files in os.walk(path): this is looping through all the directories files in root which is my path and finding the files

    for name in files: search for a filename in the files that it has searched through

    if name.endswith('.jpg'):
    filenames['jpg'].append(os.path.join(root, name))

    Please correct me if I am totally wrong

    I want to learn
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    72
    Rep Power
    12

    I want to learn


    My problem is I don't know what the right stuff is

    Cheers
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    If you're simply searching for file names containing a substring then you can use 'sub string' in 'main string'. It really depends on what you're looking for. You might also be interested in the glob module.

    http://www.python.org/doc/2.3.3/lib/module-glob.html

    Which provides *nix style file/path name searching. And well, if its good enough for *nix it cant be bad

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo