filter directory
Gerhard Häring
gh at ghaering.de
Sun May 18 07:47:05 EDT 2003
Thomas Guettler wrote:
> On Sat, May 17, 2003 at 01:11:29PM +1000, Psybar Phreak wrote:
>
>>ok - does anyone know how to :
>>
>>filter the contents of a folder depending on the extension?
>
>
> This script lists all files ending with ".txt". Is this what you want?
>
> import os
>
> my_files=[]
> for file in os.listdir("."):
> if file.lower().endswith(".txt"):
> my_files.append(file)
>
> print my_files
Better:
import glob
my_files = glob.glob("*.txt")
Also, 'file' is a builtin in Python 2.2+, so it's good practise to not
shadow it.
-- Gerhard
More information about the Python-list
mailing list