|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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))
|
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
um, just replace "name.endswith('.jpg')" with whatever string checking you want
|
|
#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 |
|
#6
|
|||
|
|||
|
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.
|
|
#7
|
|||
|
|||
|
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 |
|
#8
|
|||
|
|||
|
I want to learn
My problem is I don't know what the right stuff is
Cheers |
|
#9
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Problem searching for files |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|