I do this, mabye a no-no?
import os
for root,dirs,files in os.walk(dir) : break
--
http://mail.python.org/mailman/listinfo/python-list
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
--
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
--
http://mail.python.org/mailman/listinfo/python-list
In message [EMAIL PROTECTED], 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,
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 [EMAIL PROTECTED] --
--
http://mail.python.org/mailman/listinfo/python-list
On Jul 24, 11:52 am, Lanny [EMAIL PROTECTED] 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.
So you -only- want the files from one directory?
Try: _, _, files = os.walk('/top/folder/here').next()
The single
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
--
On Jul 23, 5:22 pm, Fredrik Lundh [EMAIL PROTECTED] 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.
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(/))
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
On Jul 23, 10:11 pm, Larry Bates [EMAIL PROTECTED] 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
On Jul 23, 10:26 pm, Fredrik Lundh [EMAIL PROTECTED] wrote:
random.choice(filter(os.path.isfile, glob.glob(/*)))
isn't that bad, though.
I'll agree to that.
--
http://mail.python.org/mailman/listinfo/python-list
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 [EMAIL PROTECTED] --
how about:
import os
x = os.walk('/')
(root,dirs,files) =
13 matches
Mail list logo