[Tutor] list.index() question

Kent Johnson kent37 at tds.net
Tue Dec 9 01:55:24 CET 2008


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 :-)

> I had a question about using the index() function on a list -- as I
> walk the directory path, I want to see if a directory contains any
> files ending in a certain type ... if it does, I wanna do some stuff
> ... if not, I would like to move on ... .

index() searches for a specific matching item, it doesn't have any
wildcard ability.

> 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" ?
>         for file in files:
>                #do some things in here to sort my files
>     except ValueError:
>         pass

I'm not sure what you want to do, but if you just want to operate on
files that end with .flac, you can just say
for file in files:
  if not file.endswith('.flac'):
    continue

though 'file' is not a good variable name, there is a builtin of that name.

> Basically: how do I make it match *.flac ?  I couldn't find anything
> on google (searching for "python index" just gets me a lot of indexes
> of python docs - wink)

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

Kent


More information about the Tutor mailing list