By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,589 Members | 1,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,589 IT Pros & Developers. It's quick & easy.

os.walk question

P: n/a
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.

Thanks.

-- Posted on news://freenews.netfront.net - Complaints to ne**@netfront.net --
Jul 23 '08 #1
Share this Question
Share on Google+
12 Replies

P: n/a
On Jul 24, 11:52*am, "Lanny" <la...@freshells.chwrote:
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.
So you -only- want the files from one directory?

Try: _, _, files = os.walk('/top/folder/here').next()

The single underscore is a convention to indicate we don't care about
those results.

The .next() is needed to step through os.walk once, which will start
at the path you specify.

Hope this helps.

- alex23
Jul 23 '08 #2

P: n/a
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>

Jul 23 '08 #3

P: n/a
On Jul 23, 5:22*pm, Fredrik Lundh <fred...@pythonware.comwrote:
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 :)
Jul 23 '08 #4

P: n/a
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
Jul 23 '08 #5

P: n/a
alex23 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.
as illustrated by my example (cut and pasted from a windows machine). oops.

random.choice(filter(os.path.isfile, glob.glob("/*")))

isn't that bad, though.

</F>

Jul 23 '08 #6

P: n/a
On Jul 23, 10:11*pm, Larry Bates <larry.ba...@websafe.com`wrote:
import glob
random.choice([f for f in glob.glob(root, "*")])
glob.glob only accepts one argument though, did you mean root + "*"?

It also returns folders as well as files, though, and the list
comprehension is not necessary given it returns a list itself.
Jul 23 '08 #7

P: n/a
On Jul 23, 10:26*pm, Fredrik Lundh <fred...@pythonware.comwrote:
* * *random.choice(filter(os.path.isfile, glob.glob("/*")))

isn't that bad, though.
I'll agree to that.
Jul 23 '08 #8

P: n/a
B
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.

Thanks.


-- Posted on news://freenews.netfront.net - Complaints to ne**@netfront.net --
how about:

import os

x = os.walk('/')
(root,dirs,files) = x.next()

'files' should contain a list of filenames.
Jul 24 '08 #9

P: n/a
In message <ma************************************@python.org >, 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'
Won't that return any subdirectories as well as files?
Jul 25 '08 #10

P: n/a
Lawrence D'Oliveiro wrote:
Won't that return any subdirectories as well as files?
sure, as was discussed in this very thread two days ago.

</F>

Jul 25 '08 #11

P: n/a
I do this, mabye a no-no?
import os

for root,dirs,files in os.walk(dir) : break
Jul 26 '08 #12

P: n/a


Eric Wertman wrote:
I do this, mabye a no-no?
It is a roundabout way to do multiple assignment:
import os
for root,dirs,files in os.walk(dir) : break
root,dirs,files = os.walk(dir).next #2.x
root,dirs,files = next(os.walk(dir))#3.x

Jul 26 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.