Sorting Directories from files in a os.listdir??

S

Soren

Hi,

I'd like to read the filenames in a directory, but not the
subdirectories, os.listdir() gives me everything... how do I separate
the directory names from the filenames? Is there another way of doing
this?

Thanks!
 
D

David Harrison

Hi,

I'd like to read the filenames in a directory, but not the
subdirectories, os.listdir() gives me everything... how do I separate
the directory names from the filenames? Is there another way of doing
this?

The only thing I can think of if you just want the immediate dir is to
use the os.path module's function isfile to test each item from the
list returned by os.listdir.

This should do the trick I think:

[ f for f in os.listdir('pathname') if os.path.isfile(f) ]

cheers
Dave
 
G

Gabriel Genellina

I'd like to read the filenames in a directory, but not the
subdirectories, os.listdir() gives me everything... how do I separate
the directory names from the filenames? Is there another way of doing
this?

Check each returned name using os.path.isfile
(untested):

def files_only(path):
return [filename for filename in os.listdir(path)
if os.path.isfile(os.path.join(path, filename))]
 
G

Giampaolo Rodola'

Hi,

I'd like to read the filenames in a directory, but not the
subdirectories, os.listdir() gives me everything... how do I separate
the directory names from the filenames? Is there another way of doing
this?

Thanks!

I guess you have no other way than using os.path.isfile or
os.path.isdir for every entry returned by os.listdir.


--- Giampaolo
http://code.google.com/p/pyftpdlib
 
S

Soren

I'd like to read the filenames in a directory, but not the
subdirectories, os.listdir() gives me everything... how do I separate
the directory names from the filenames? Is there another way of doing
this?

Check each returned name using os.path.isfile
(untested):

def files_only(path):
return [filename for filename in os.listdir(path)
if os.path.isfile(os.path.join(path, filename))]

Thanks everyone! That worked! :)
 
P

Peter Otten

Scott said:
I'd like to read the filenames in a directory, but not the
subdirectories, os.listdir() gives me everything... how do I separate
the directory names from the filenames? Is there another way of doing
this?

Thanks!

import os
base, files, dirs = iter(os.walk(dirname)).next()
# now files is files and dirs is directories (and base == dirname)
import os
os.listdir(".") ['fifo', 'dir', 'file'] # a fifo, a directory, and a file
w = os.walk(".")
w is iter(w) # calling iter() is a noop here True
w.next()
('.', ['dir'], ['fifo', 'file']) # base, dirs, files; everything that is not
# a directory goes into the files list

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top