[Tutor] list.index() question

Damon Timm damontimm at gmail.com
Tue Dec 9 02:17:15 CET 2008


On Mon, Dec 8, 2008 at 7:55 PM, Kent Johnson <kent37 at tds.net> wrote:
> index() searches for a specific matching item, it doesn't have any
> wildcard ability.

Ah ha!

> There is actually an index:
> http://docs.python.org/genindex.html

Heh heh - and the info I was looking for is at:
http://docs.python.org/library/stdtypes.html#index-584 ... I've become
google dependent ... if it's not on google I don't know where to look.

Thanks for the .endswith() tip.

On 12/8/08 7:47 PM, Alan Gauld wrote:
> Check out the glob module.
>
>> for dirpath, subFolders, files in os.walk(rootDir):
>>     try:
>>         i = files.index("*.flac") #how do I make it search for files
>> that end in ".flac" ?
>
> If yu call glob.glob() with the dirpath you will get a list of all
> the flac files in the current dir.

Heading to check out glob.glob() now ...

On 12/8/08 7:29 PM, John Fouhy wrote:
> The fnmatch module will help here.  It basically implements unix-style
> filename patterns.  For example:
>
> import os
> import fnmatch
>
> files = os.listdir('.')
> flac_files = fnmatch(files, '*.flac')
>
> So, to test whether you have any flac files, you can just test whether
> fnmatch(files, '*.flac') is empty.
>
> If you wanted to roll your own solution (the fnmatch module is a bit
> obscure, I think), you could do something with os.path.splitext:
>
> files = os.listdir('.')
> extensions = [os.path.splitext(f)[1] for f in files]
> if '.flac' in extensions:
>   print 'FLAC files found!'

And then to look at fnmatch!

Thanks for the direction -- on my way ...

On 12/8/08 7:55 PM, Kent Johnson wrote:
> On Mon, Dec 8, 2008 at 7:05 PM, Damon Timm <damontimm at gmail.com> wrote:
>> Hi again!
>>
>> (Now that everyone was so helpful the first time you'll never get rid of me!)
>
> That's fine, pretty soon you'll be answering other people's questions :-)

Not quite there yet ... one day, maybe.  I can show people where the
index for index is!

Damon


More information about the Tutor mailing list