os.walk question
alex23
wuwei23 at gmail.com
Wed Jul 23 07:48:36 EDT 2008
On Jul 23, 5:22 pm, Fredrik Lundh <fred... at pythonware.com> wrote:
> if you want a list of files from a single directory, use listdir, not walk:
>
> >>> import os, random
> >>> random.choice(os.listdir("/"))
> 'python25'
This will include folders as well, though, which isn't what the OP
asked for.
>>> import os, random
>>> root = "/"
>>> random.choice([f for f in os.listdir(root) if os.path.isfile(os.path.join(root, f))])
'initrd.img'
It just seems clunky compared to os.walk :)
More information about the Python-list
mailing list