f



How to separate directory list and file list?

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
0
Gonnasi
10/23/2005 3:09:46 PM
comp.lang.python 77058 articles. 6 followers. Post Follow

6 Replies
613 Views

Similar Articles

[PageSpeed] 35

Hi,

2005/10/23, Gonnasi <harddong@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 =3D "/home/test"
files =3D [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 <oliver.andrich@gmail.com> --- http://roughbook.de/
0
10/23/2005 3:25:11 PM
"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>



0
fredrik2101 (5275)
10/23/2005 3:27:49 PM
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
0
peter34 (3696)
10/23/2005 4:23:14 PM
"Gonnasi" <harddong@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


0
gsakkis (345)
10/23/2005 4:31:00 PM
Gonnasi <harddong@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
0
aleaxit (1612)
10/23/2005 4:43:45 PM
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"
<fredrik@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>
>
>

0
Gonnasi
10/24/2005 1:07:04 PM
Reply: