Partial directory search question

lallous lallous at lgwm.org
Wed Sep 30 09:19:07 CEST 2009


"chad" <cdalten at gmail.com> wrote in message 
news:4e260ef3-8b0e-4613-a4f8-1c267e8754e6 at u16g2000pru.googlegroups.com...
> On Sep 29, 7:20 pm, Tim Chase <python.l... at tim.thechases.com> wrote:
>> > What's the sanest way to print out all the files in the directory that
>> > start with the underscore? Ie, I just want to list _1, _2, _3, _4.
>>
>> I'd use a string's join() method to combine the results of a
>> list-comprehension or generator that filtered the output of
>> os.listdir() based on the startswith() method of the strings.
>>
>> Left intentionally oblique and code-free because this sounds a
>> bit like a home-work problem.  If you're a python coder, that
>> should make pretty decent sense and be a one-liner to implement.
>>
>> -tkc
>
> Okay, sorry for the delay to the response. I got side tracked trying
> to stalk, I mean talk to the 59 year old neighbor girl. Anyways, I
> couldn't get it to one in one line. Here is what I did...
>
> % more rec.py
> #!/usr/local/bin/python
>
> import os
> import time
>
> for filename in os.listdir("/usr/bbs/confs/september"):
>     #stat = os.stat(filename)
>     if filename.startswith("_"):
>        print filename
>

L = [filename for filename in os.listdir("/usr/bbs/confs/september") if 
filename.startswith("_")]

Now you have a list with the desired filtered names.

> ./rec.py
> _1
> _2
> _3
> _4
> _5
> _6
> _7
> _8
>
> It correctly prints out all the files in the directory that start with
> an underscore. 




More information about the Python-list mailing list