[Tutor] Help return a pattern from list

Thomas C. Hicks paradox at pobox.com
Tue Jul 6 01:40:28 CEST 2010


On Mon, 05 Jul 2010 20:29:02 +0200
tutor-request at python.org wrote:

> Date: Mon, 5 Jul 2010 13:54:55 -0400
> From: Vineeth Rakesh <vineethrakesh at gmail.com>
> To: tutor at python.org
> Subject: [Tutor] Help return a pattern from list
> Message-ID:
> 	<AANLkTimtfL0HdRJQrRZJxSyv8VeBwEHpvz_QDcJG4ftM at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> Hello all,
> 
> Can some one help me to return a special pattern from a list.
> 
> say list =
> ["something1.mp3","something2.mp3","something4.pdf","something5.odt"]
> 
> now say I just need to return the files with .mp3 extension. How to
> go about doing this?
> 
> Thanks
> Vin

I use the fnmatch module:

import fnmatch
fileList =
["something1.mp3","something2.mp3","something4.pdf","something5.odt"]
pattern='*.mp3'
for x in fnmatch.filter(fileList,pattern):
	#do something to your files or list items here

thomas


More information about the Tutor mailing list