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

Sorting Directories from files in a os.listdir??

P: n/a
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!
Apr 10 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 10/04/2008, Soren <so****************@gmail.comwrote:
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
Apr 10 '08 #2

P: n/a
On 10 Apr, 11:55, Soren <soren.skou.niel...@gmail.comwrote:
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
Apr 10 '08 #3

P: n/a
En Thu, 10 Apr 2008 06:55:13 -0300, Soren <so****************@gmail.com>
escribió:
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))]

--
Gabriel Genellina

Apr 10 '08 #4

P: n/a
On Apr 10, 12:14 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Thu, 10 Apr 2008 06:55:13 -0300, Soren <soren.skou.niel...@gmail.com>
escribió:
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))]

--
Gabriel Genellina
Thanks everyone! That worked! :)
Apr 10 '08 #5

P: n/a
Soren wrote:
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!
import os
base, files, dirs = iter(os.walk(dirname)).next()
# now files is files and dirs is directories (and base == dirname)
Apr 10 '08 #6

P: n/a
Scott David Daniels wrote:
>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
Apr 10 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.