SunQuest
           Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old February 8th, 2004, 02:24 PM
maboroshi maboroshi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 48 maboroshi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 30 m 29 sec
Reputation Power: 5
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

Reply With Quote
  #2  
Old February 8th, 2004, 03:47 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
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))

Reply With Quote
  #3  
Old February 8th, 2004, 04:59 PM
maboroshi maboroshi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 48 maboroshi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 30 m 29 sec
Reputation Power: 5
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

Reply With Quote
  #4  
Old February 8th, 2004, 05:39 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
um, just replace "name.endswith('.jpg')" with whatever string checking you want

Reply With Quote
  #5  
Old February 8th, 2004, 06:06 PM
maboroshi maboroshi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 48 maboroshi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 30 m 29 sec
Reputation Power: 5
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

Reply With Quote
  #6  
Old February 8th, 2004, 06:59 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
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.

Reply With Quote
  #7  
Old February 8th, 2004, 07:23 PM
maboroshi maboroshi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 48 maboroshi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 30 m 29 sec
Reputation Power: 5
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

Reply With Quote
  #8  
Old February 8th, 2004, 07:26 PM
maboroshi maboroshi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 48 maboroshi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 30 m 29 sec
Reputation Power: 5
I want to learn

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

Cheers

Reply With Quote
  #9  
Old February 8th, 2004, 07:37 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Problem searching for files


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway