ls files --> list packer

I V wrongbad at gmail.com
Fri Feb 24 02:45:11 EST 2006


kpp9c wrote:
> Namely i have organized a bunch of folders that have soundfiles in them
> and would like Python to slurp up all the .aif/.aiff (or .wav whatever)
> files in a given set of directories. My friend hacked up this is perl:
>
> $files = `ls /snd/Public/*.aiff`;

You could use posix.popen to duplicate the perl hack:

files = posix.popen('ls /snd/Public/*.aiff').read().strip()

> @snd_filelist = split('\n',$files);

snd_filelist = files.split('\n')

> I would like something similar, that works with python that is more
> elegant and maybe even more robust.

Lucklily, python lets you avoid this kind of horrible hack. Try
os.listdir:

snd_filelist = [f for f in os.listdir('/snd/Public/') if
f.endswith('.aiff')]

I think it's more elegant, and it's certainly more robust.

> but i am failing miserably and my perl friends mock me.

Now you get to mock your perl friends!




More information about the Python-list mailing list