468,294 Members | 1,830 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,294 developers. It's quick & easy.

How to separate directory list and file list?

With
glob.glob("*")
oros.listdir(cwd)


I can get a combined file list with directory list, but I just wanna a
bare file list, no directory list. How to get it?

Tons of thanks in advance!

Gonnasi
Oct 23 '05 #1
6 2573
Hi,

2005/10/23, Gonnasi <ha******@21cn.net>:
With
glob.glob("*")


or
os.listdir(cwd)


I can get a combined file list with directory list, but I just wanna a
bare file list, no directory list. How to get it?


don't know if it is the best solution, but it looks nice. :)

path = "/home/test"
files = [fn for fn in os.listdir(path) if
os.path.isfile(os.path.join(path, fn))]

This gives you just the list of files in a given directory.

Best regards,
Oliver

--
Oliver Andrich <ol************@gmail.com> --- http://roughbook.de/
Oct 23 '05 #2
"Gonnasi" wrote:
With
glob.glob("*")


or
os.listdir(cwd)


I can get a combined file list with directory list, but I just wanna a
bare file list, no directory list. How to get it?


use os.path.isfile on the result.

for file in glob.glob("*"):
if not os.path.isfile(file):
continue
... deal with file ...

for file in os.listdir(cwd):
file = os.path.join(cwd, file)
if not os.path.isfile(file):
continue
... deal with file ...

files = map(os.path.isfile, glob.glob("*"))

files = (file for file in os.listdir(cwd) if os.path.isfile(os.path.join(cwd, file)))

etc.

</F>

Oct 23 '05 #3
Gonnasi wrote:
With
glob.glob("*")

or
os.listdir(cwd)


I can get a combined file list with directory list, but I just wanna a
bare file list, no directory list. How to get it?


Using Jason Orendorff's path module, it's just this:
from path import path
path('.').files() # returns list of files in current dir

-Peter
Oct 23 '05 #4
"Gonnasi" <ha******@21cn.net> wrote:
With
glob.glob("*")


or
os.listdir(cwd)


I can get a combined file list with directory list, but I just wanna a
bare file list, no directory list. How to get it?

Tons of thanks in advance!

Gonnasi


Using the path module (http://www.jorendorff.com/articles/python/path/):

from path import path
path('.').files()

George
Oct 23 '05 #5
Gonnasi <ha******@21cn.net> wrote:
With
glob.glob("*")


or
os.listdir(cwd)


I can get a combined file list with directory list, but I just wanna a
bare file list, no directory list. How to get it?


I see everybody's suggesting os.path.* solutions, and they're fine, but
an interesting alternative is os.walk:

__, thedirs, thefiles = os.walk('.').next()

thefiles is the list of filenames (and thedirs is the list of directory
names), and each is sorted alphabetically. (I'm assigning to '__' the
absolute path of the current directory, meaning I intend to ignore it).
An expression that just provides the filename list is

os.walk('.').next()[2]

although this may be a tad too obscure to recommend it!-)
Alex
Oct 23 '05 #6
Lots of thanks for your help, My code can return the right result now.

Thanks again!

On Sun, 23 Oct 2005 17:27:49 +0200, "Fredrik Lundh"
<fr*****@pythonware.com> wrote:
"Gonnasi" wrote:
With
>glob.glob("*")


or
>os.listdir(cwd)


I can get a combined file list with directory list, but I just wanna a
bare file list, no directory list. How to get it?


use os.path.isfile on the result.

for file in glob.glob("*"):
if not os.path.isfile(file):
continue
... deal with file ...

for file in os.listdir(cwd):
file = os.path.join(cwd, file)
if not os.path.isfile(file):
continue
... deal with file ...

files = map(os.path.isfile, glob.glob("*"))

files = (file for file in os.listdir(cwd) if os.path.isfile(os.path.join(cwd, file)))

etc.

</F>


Oct 24 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

39 posts views Thread by Joe Laughlin | last post: by
3 posts views Thread by Patrick Johnson | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.