[Tutor] fnmatch -or glob question
Damon Timm
damontimm at gmail.com
Sat Jul 4 13:37:01 CEST 2009
Hi Kent and Allen - thanks for the responses ... I was sure there was
some part of the "search" I wasn't getting, but with your follow-up
questions it seemed something else was amiss ...
So, I went back to re-create the problem with some more python output
to show you and realized my mistake. Sigh. All the files were
DSC_00XX ... and then one I had chosen to test didn't have any
thumbnails. So, of course, I wasn't getting any responses ... though,
because the filenames were so close, I sure thought I should have
been.
Good lesson here is: check the variables !
My goal is to delete the main image plus its thumbnails in one fell
swoop -- I plan to use fnmatch to do that (though glob works too).
Unless there is something else I should consider?
For those who stumble like me, here is what is working.
Python 2.5.2 (r252:60911, Jul 31 2008, 17:31:22)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from kissfist.read.models import Issue #custom model that stores issue info
>>> import os, fnmatch, glob
>>> i = Issue.objects.get(pk=1) # get the first issue (contains cover path)
>>> i.cover.path
'/media/uploads/issue-covers/DSC_0065.jpg'
>>> working_dir, file_name = os.path.split(i.cover.path)
>>> file_base, file_ext = os.path.splitext(file_name)
>>> glob_text = file_base + "*" + file_ext
>>> for f in os.listdir(working_dir):
... if fnmatch.fnmatch(f, glob_text):
... print f
...
DSC_0065.400x400.jpg
DSC_0065.jpg
DSC_0065.300.jpg
>>> os.chdir(working_dir)
>>> glob.glob(glob_text)
['DSC_0065.400x400.jpg', 'DSC_0065.jpg', 'DSC_0065.300.jpg']
Thanks again.
Damon
On Sat, Jul 4, 2009 at 4:27 AM, Alan Gauld<alan.gauld at btinternet.com> wrote:
>
> "Damon Timm" <damontimm at gmail.com> wrote
>
>> And I thought I could just construct something for glob or fnmatch like:
>>
>> glob.glob("DSC_0065*.jpg") --or-- fnmatch.fnmatch(file, "DSC_0065*.jpg")
>>
>> But I'm not getting anywhere.
>
> Can you give is a clue as to what you are getting?
> What is happening and what do you experct to happen?
> Are you finding any files? some files? too many files?
> Do you get an error message?
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list