os.walk question

Larry Bates larry.bates at websafe.com`
Wed Jul 23 08:11:26 EDT 2008


Fredrik Lundh wrote:
> Lanny wrote:
> 
>> How would one make a list of the files in the top directory
>> using os.walk.
>>
>> I need to pick a random file from said list.
> 
> if you want a list of files from a single directory, use listdir, not walk:
> 
>      >>> import os, random
>      >>> random.choice(os.listdir("/"))
>      'python25'
> 
> </F>
> 
Or use glob.

import glob
random.choice([f for f in glob.glob(root, "*")])

-Larry



More information about the Python-list mailing list