By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

Searching

P: n/a
ADE
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(self):
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.insert(END, name)
elif name.endswith('.gif'): #As well as this
filenames['gif'].append(os.path.join(root, name))
self.listbox1.insert(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

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ADE wrote:
Hi I was wondering if one of you could help me out You are missing info on your system and python version.
If you are running 2.3 or later:...
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.insert(END, name)
elif name.endswith('.gif') and 'ocean' in name:
filenames['gif'].append(os.path.join(root, name))
self.listbox1.insert(END, name)


--
-Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #2

P: n/a
ADE
Hi sorry about that I am running Python 2.3 and Windows XP

"Scott David Daniels" <Sc***********@Acm.Org> wrote in message
news:40********@nntp0.pdx.net...
ADE wrote:
Hi I was wondering if one of you could help me out

You are missing info on your system and python version.
If you are running 2.3 or later:
...
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.insert(END, name)
elif name.endswith('.gif') and 'ocean' in name:
filenames['gif'].append(os.path.join(root, name))
self.listbox1.insert(END, name)


--
-Scott David Daniels
Sc***********@Acm.Org

Jul 18 '05 #3

P: n/a
Hello,
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

Have a look at the `glob' module.

HTH.
Miki
Jul 18 '05 #4

P: n/a
Perhaps better (adds '.png' more easily, keeps original case):
...
for name in files:
lname = name.lower() # if on windows
for ext, names in filenames.iteritems():
if lname.endswith(ext) and 'ocean' in name:
names.append(os.path.join(root, name))
self.listbox1.insert(END, name)
break
...
--
-Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.