Re: [Tutor] list packing
kevin parks wrote: > John, > > Thanks... i am liking this variation a tad more since it means i only > have to type the path in one place but it is akin to your second > one... i was (still am really) having a hard time understanding > how to apply path.join _and_ listdir sometimes list comprehensions > twist my brain but this one is worth studying ... > > nice! two little lines that do a boatload of work! hee hee > > > pth = '/Users/kpp9c/snd/01' > samples = [os.path.join(pth, f) for f in os.listdir(pth) if f.endswith > ('.aif')] > > > i could even add: > samples = [os.path.join(pth, f) for f in os.listdir(pth) if f.endswith > ('.aif' & f.startswith('t')] You might like Jason Orendorff's path module. Using it you can write import path pth = path.path('/Users/kpp9c/snd/01') samples = list(pth.walkfiles('t*.aif')) path.walkfiles() returns an iterator, that is why the list() call is needed. If you are going to loop over the samples you can omit it: for sample in pth.walkfiles('t*.aif'): ... http://www.jorendorff.com/articles/python/path/index.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list packing
Sean Perry wrote: > os.path.join() is self-documenting. I find this to be a better reason to > use it than anything else. But then my code only ever runs on Unix of > some flavor. I'm not sure why you put in the comment about Unix - os.path.join() is the recommended way of joining paths portably - it will do the right thing on any supported platform. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list packing
John, Thanks... i am liking this variation a tad more since it means i only have to type the path in one place but it is akin to your second one... i was (still am really) having a hard time understanding how to apply path.join _and_ listdir sometimes list comprehensions twist my brain but this one is worth studying ... nice! two little lines that do a boatload of work! hee hee pth = '/Users/kpp9c/snd/01' samples = [os.path.join(pth, f) for f in os.listdir(pth) if f.endswith ('.aif')] i could even add: samples = [os.path.join(pth, f) for f in os.listdir(pth) if f.endswith ('.aif' & f.startswith('t')] to make sublists in guess What i like about this is that i can chance my directory structure (add/delete/rename/etc) and shape it any way that i want and then Python reflects all my restructuring... lovely... Exactly the type of tedious thing i want Python to do for me *^-^* cheers, kevin ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list packing
John Fouhy wrote: > On 27/02/06, kevin parks <[EMAIL PROTECTED]> wrote: > >>snd = [f for f in os.listdir('/Users/kevin/snd/') if f.endswith('.aif')] > > > If this is all you need, then you could do something like: > > snd = ['/Users/kevin/snd/%s' % f for f in > os.listdir('/Users/kevin/snd/') if f.endswith('.aif')] > > Or, slightly more robustly (?), > > snd = [os.path.join('/Users/kevin/snd', f) for f in > os.listdir('/Users/kevin/snd/') if f.endswith('.aif')] > os.path.join() is self-documenting. I find this to be a better reason to use it than anything else. But then my code only ever runs on Unix of some flavor. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list packing
On 27/02/06, kevin parks <[EMAIL PROTECTED]> wrote: > snd = [f for f in os.listdir('/Users/kevin/snd/') if f.endswith('.aif')] If this is all you need, then you could do something like: snd = ['/Users/kevin/snd/%s' % f for f in os.listdir('/Users/kevin/snd/') if f.endswith('.aif')] Or, slightly more robustly (?), snd = [os.path.join('/Users/kevin/snd', f) for f in os.listdir('/Users/kevin/snd/') if f.endswith('.aif')] -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor