[Tutor] list.index() question
John Fouhy
john at fouhy.net
Tue Dec 9 01:29:54 CET 2008
On 09/12/2008, Damon Timm <damontimm at gmail.com> wrote:
> 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)
Hi Damon,
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!'
HTH!
--
John.
More information about the Tutor
mailing list