directory listing

Discussion in 'Python' started by SU News Server, Nov 11, 2005.

  1. I've struggled with this for quite a while and I'm am just not sure
    what is going on. I have the following code
    import os

    def buildList( directory='/Users/mkonrad' )

    dirs = [ ]

    listing = os.listdir(directory)

    for x in listing:
    if os.path.isdir(x):
    dirs.append(x)

    return dirs

    This always returns an empty list.
    Now if I change it so that directory='.' or directory=os.getcwd()
    Then it returns a list of directories.

    Any ideas?

    Thank you,
    -Michael
    SU News Server, Nov 11, 2005
    #1
    1. Advertising

  2. "SU News Server" <> wrote:

    > I've struggled with this for quite a while and I'm am just not sure
    > what is going on. I have the following code
    > import os
    >
    > def buildList( directory='/Users/mkonrad' )
    >
    > dirs = [ ]
    >
    > listing = os.listdir(directory)
    >
    > for x in listing:
    > if os.path.isdir(x):
    > dirs.append(x)
    >
    > return dirs
    >
    > This always returns an empty list.
    > Now if I change it so that directory='.' or directory=os.getcwd()
    > Then it returns a list of directories.


    hint: if you're not sure what's going on in your program, adding
    a print statement or two is an easy way to figure it out. like, say:

    for x in listing:
    print x
    if os.path.isdir(x):
    dirs.append(x)

    (try this before you continue)

    :
    :
    :

    the problem is that os.listdir returns a list of filenames, without
    the preceeding directory name. you can add an os.path.join

    for x in listing:
    x = os.path.join(directory, x)
    if os.path.isdir(x):
    dirs.append(x)

    or use the glob module instead:

    listing = glob.glob(os.path.join(directory, "*"))

    hope this helps!

    </F>
    Fredrik Lundh, Nov 11, 2005
    #2
    1. Advertising

  3. On 11 Nov 2005 21:20:33 GMT, SU News Server wrote:

    Try passing the full pathname of each item to os.path.isdir()

    You can create the pathname using os.path.join(directory, x)


    --
    Richard
    Richard Townsend, Nov 11, 2005
    #3
  4. "Fredrik Lundh" <> wrote:

    > "SU News Server" <> wrote:
    >
    >> I've struggled with this for quite a while and I'm am just not sure
    >> what is going on. I have the following code
    >> import os
    >>
    >> def buildList( directory='/Users/mkonrad' )
    >>
    >> dirs = [ ]
    >>
    >> listing = os.listdir(directory)
    >>
    >> for x in listing:
    >> if os.path.isdir(x):
    >> dirs.append(x)
    >>
    >> return dirs
    >>
    >> This always returns an empty list.
    >> Now if I change it so that directory='.' or directory=os.getcwd()
    >> Then it returns a list of directories.

    >
    > hint: if you're not sure what's going on in your program, adding
    > a print statement or two is an easy way to figure it out. like, say:
    >
    > for x in listing:
    > print x
    > if os.path.isdir(x):
    > dirs.append(x)
    >


    Did that and I was just getting a bunch of [ ].

    > (try this before you continue)
    >
    > :
    > :
    > :
    >
    > the problem is that os.listdir returns a list of filenames, without
    > the preceeding directory name. you can add an os.path.join
    >
    > for x in listing:
    > x = os.path.join(directory, x)
    > if os.path.isdir(x):
    > dirs.append(x)


    OK. This works except each entry in dirs now includes the full path.
    Is there an unjoin? :) I haven't spent any time trying to work this out.

    >
    > or use the glob module instead:
    >
    > listing = glob.glob(os.path.join(directory, "*"))
    >
    > hope this helps!
    >
    > </F>
    >
    >
    >
    >
    Michael Konrad, Nov 11, 2005
    #4
  5. Richard Townsend <> wrote:

    > On 11 Nov 2005 21:20:33 GMT, SU News Server wrote:
    >
    > Try passing the full pathname of each item to os.path.isdir()
    >
    > You can create the pathname using os.path.join(directory, x)
    >
    >
    >


    I wonder if I can join ./, so I don't have the full path in each
    entry?

    Thank you for responding.
    _Michael
    Michael Konrad, Nov 11, 2005
    #5
  6. On 11 Nov 2005 22:00:04 GMT, Michael Konrad wrote:

    > Richard Townsend <> wrote:
    >
    >> On 11 Nov 2005 21:20:33 GMT, SU News Server wrote:
    >>
    >> Try passing the full pathname of each item to os.path.isdir()
    >>
    >> You can create the pathname using os.path.join(directory, x)
    >>
    >>
    >>

    >
    > I wonder if I can join ./, so I don't have the full path in each
    > entry?
    >
    > Thank you for responding.
    > _Michael


    Why not assign the os.path.join() result to a new variable, pass that to
    os.path.isdir() but still append x to the list?


    --
    Richard
    Richard Townsend, Nov 11, 2005
    #6
  7. Michael Konrad wrote:

    > > for x in listing:
    > > print x
    > > if os.path.isdir(x):
    > > dirs.append(x)
    > >

    >
    > Did that and I was just getting a bunch of [ ].


    if you printed "x" (the filename), that doesn't sound very likely.
    maybe you printed some other variable? (like "dirs")

    > > for x in listing:
    > > x = os.path.join(directory, x)
    > > if os.path.isdir(x):
    > > dirs.append(x)

    >
    > OK. This works except each entry in dirs now includes the full path.
    > Is there an unjoin? :)


    use two variables:

    for name in listing:
    fullname = os.path.join(directory, name)
    if os.path.isdir(fullname):
    dirs.append(name)

    </F>
    Fredrik Lundh, Nov 11, 2005
    #7
  8. SU News Server

    Shi Mu Guest

    On 11 Nov 2005 22:00:04 GMT, Michael Konrad <> wrote:
    > Richard Townsend <> wrote:
    >
    > > On 11 Nov 2005 21:20:33 GMT, SU News Server wrote:
    > >
    > > Try passing the full pathname of each item to os.path.isdir()
    > >
    > > You can create the pathname using os.path.join(directory, x)
    > >
    > >
    > >

    >
    > I wonder if I can join ./, so I don't have the full path in each
    > entry?
    >
    > Thank you for responding.
    > _Michael
    >
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >

    I tried this and no error reported but nothing appear on the console, why?

    import os

    def buildList( directory='c:\TEMP' ):
    dirs = [ ]
    listing = os.listdir(directory)
    for x in listing:
    x = os.path.join(directory, x)
    print x
    if os.path.isdir(x):
    dirs.append(x)
    return dirs
    Shi Mu, Nov 11, 2005
    #8
  9. Shi Mu wrote:

    > I tried this and no error reported but nothing appear on the console, why?
    >
    > import os
    >
    > def buildList( directory='c:\TEMP' ):
    > dirs = [ ]
    > listing = os.listdir(directory)
    > for x in listing:
    > x = os.path.join(directory, x)
    > print x
    > if os.path.isdir(x):
    > dirs.append(x)
    > return dirs


    is that the entire script? you're defining a function, but you're
    not calling it. try adding

    print buildList()

    at the end of the script.

    </F>
    Fredrik Lundh, Nov 11, 2005
    #9
  10. SU News Server

    Shi Mu Guest

    On 11/11/05, Fredrik Lundh <> wrote:
    > Shi Mu wrote:
    >
    > > I tried this and no error reported but nothing appear on the console, why?
    > >
    > > import os
    > >
    > > def buildList( directory='c:\TEMP' ):
    > > dirs = [ ]
    > > listing = os.listdir(directory)
    > > for x in listing:
    > > x = os.path.join(directory, x)
    > > print x
    > > if os.path.isdir(x):
    > > dirs.append(x)
    > > return dirs

    >
    > is that the entire script? you're defining a function, but you're
    > not calling it. try adding
    >
    > print buildList()
    >
    > at the end of the script.


    It works but i am curious why the line of "print x" does not show
    anything. many thanks!
    Shi Mu, Nov 11, 2005
    #10
  11. "Shi Mu" wrote:

    > but i am curious why the line of "print x" does not show
    > anything.


    because your c:\temp directory is empty ?

    </F>
    Fredrik Lundh, Nov 11, 2005
    #11
  12. SU News Server

    Shi Mu Guest

    On 11/11/05, Fredrik Lundh <> wrote:
    > "Shi Mu" wrote:
    >
    > > but i am curious why the line of "print x" does not show
    > > anything.

    >
    > because your c:\temp directory is empty ?
    >
    > </F>

    print buildList() gets lots of stuffs from my temp directory(there do
    exist lots of files).
    But why "print x' has nothing?
    Shi Mu, Nov 11, 2005
    #12
  13. "Shi Mu" wrote:

    > print buildList() gets lots of stuffs from my temp directory(there do
    > exist lots of files).
    > But why "print x' has nothing?


    C:\>more script.py
    import os

    def buildList( directory='c:\TEMP' ):
    dirs = [ ]
    listing = os.listdir(directory)
    for x in listing:
    x = os.path.join(directory, x)
    print x
    if os.path.isdir(x):
    dirs.append(x)
    return dirs

    print buildList()

    C:\>dir temp
    ....

    2005-11-12 00:00 <KAT> .
    2005-11-12 00:00 <KAT> ..
    2005-11-12 00:00 20 bacon.dat
    2005-11-12 00:00 <KAT> egg
    2005-11-12 00:00 20 spam.txt
    2 fil(er) 40 byte
    3 katalog(er) 9 818 021 888 byte ledigt

    C:\>python script.py
    c:\TEMP\bacon.dat
    c:\TEMP\egg
    c:\TEMP\spam.txt
    ['c:\\TEMP\\egg']

    </F>
    Fredrik Lundh, Nov 11, 2005
    #13
  14. SU News Server

    Peter Hansen Guest

    Shi Mu wrote:
    > On 11/11/05, Fredrik Lundh <> wrote:
    >>Shi Mu wrote:
    >>>def buildList( directory='c:\TEMP' ):
    >>> dirs = [ ]
    >>> listing = os.listdir(directory)
    >>> for x in listing:
    >>> x = os.path.join(directory, x)
    >>> print x
    >>> if os.path.isdir(x):
    >>> dirs.append(x)
    >>> return dirs

    >
    > It works but i am curious why the line of "print x" does not show
    > anything. many thanks!


    Did you use directory='c:\TEMP' as shown above, or directory='c:\temp' ?
    If you used the lower case version, you are not really checking the
    temp directory, since \t represents a TAB character. If that's the
    case, try using a forward slash instead: c:/temp .

    -Peter
    Peter Hansen, Nov 12, 2005
    #14
  15. This is what I decided on for a solution. I haven't tested it
    cross-platform yet.

    import os

    def dirListing(directory='/Users/mkonrad'):
    """Returns a list of directories."""
    #variables
    dirs = [] #list of directories

    #list of directories and files
    listing = os.listdir(directory)

    #get just the directories
    for x in listing:
    if os.path.isdir(directory+os.sep+x):
    dirs.append(x)

    return dirs

    Fredrik Lundh wrote:
    > "Shi Mu" wrote:
    >
    >> print buildList() gets lots of stuffs from my temp directory(there do
    >> exist lots of files).
    >> But why "print x' has nothing?

    >
    > C:\>more script.py
    > import os
    >
    > def buildList( directory='c:\TEMP' ):
    > dirs = [ ]
    > listing = os.listdir(directory)
    > for x in listing:
    > x = os.path.join(directory, x)
    > print x
    > if os.path.isdir(x):
    > dirs.append(x)
    > return dirs
    >
    > print buildList()
    >
    > C:\>dir temp
    > ...
    >
    > 2005-11-12 00:00 <KAT> .
    > 2005-11-12 00:00 <KAT> ..
    > 2005-11-12 00:00 20 bacon.dat
    > 2005-11-12 00:00 <KAT> egg
    > 2005-11-12 00:00 20 spam.txt
    > 2 fil(er) 40 byte
    > 3 katalog(er) 9 818 021 888 byte ledigt
    >
    > C:\>python script.py
    > c:\TEMP\bacon.dat
    > c:\TEMP\egg
    > c:\TEMP\spam.txt
    > ['c:\\TEMP\\egg']
    >
    > </F>
    >
    >
    >
    Michael Konrad, Nov 12, 2005
    #15
  16. This is what I decided on for a solution. I haven't tested it
    cross-platform yet.

    import os

    def dirListing(directory='/Users/mkonrad'):
    """Returns a list of directories."""
    #variables
    dirs = [] #list of directories

    #list of directories and files
    listing = os.listdir(directory)

    #get just the directories
    for x in listing:
    if os.path.isdir(directory+os.sep+x):
    dirs.append(x)

    return dirs

    Fredrik Lundh wrote:
    > "Shi Mu" wrote:
    >
    >> print buildList() gets lots of stuffs from my temp directory(there do
    >> exist lots of files).
    >> But why "print x' has nothing?

    >
    > C:\>more script.py
    > import os
    >
    > def buildList( directory='c:\TEMP' ):
    > dirs = [ ]
    > listing = os.listdir(directory)
    > for x in listing:
    > x = os.path.join(directory, x)
    > print x
    > if os.path.isdir(x):
    > dirs.append(x)
    > return dirs
    >
    > print buildList()
    >
    > C:\>dir temp
    > ...
    >
    > 2005-11-12 00:00 <KAT> .
    > 2005-11-12 00:00 <KAT> ..
    > 2005-11-12 00:00 20 bacon.dat
    > 2005-11-12 00:00 <KAT> egg
    > 2005-11-12 00:00 20 spam.txt
    > 2 fil(er) 40 byte
    > 3 katalog(er) 9 818 021 888 byte ledigt
    >
    > C:\>python script.py
    > c:\TEMP\bacon.dat
    > c:\TEMP\egg
    > c:\TEMP\spam.txt
    > ['c:\\TEMP\\egg']
    >
    > </F>
    >
    >
    >
    Michael Konrad, Nov 12, 2005
    #16
  17. This is what I decided on for a solution. I haven't tested it
    cross-platform yet.

    import os

    def dirListing(directory='/Users/mkonrad'):
    """Returns a list of directories."""
    #variables
    dirs = [] #list of directories

    #list of directories and files
    listing = os.listdir(directory)

    #get just the directories
    for x in listing:
    if os.path.isdir(directory+os.sep+x):
    dirs.append(x)

    return dirs

    Fredrik Lundh wrote:
    > "Shi Mu" wrote:
    >
    >> print buildList() gets lots of stuffs from my temp directory(there do
    >> exist lots of files).
    >> But why "print x' has nothing?

    >
    > C:\>more script.py
    > import os
    >
    > def buildList( directory='c:\TEMP' ):
    > dirs = [ ]
    > listing = os.listdir(directory)
    > for x in listing:
    > x = os.path.join(directory, x)
    > print x
    > if os.path.isdir(x):
    > dirs.append(x)
    > return dirs
    >
    > print buildList()
    >
    > C:\>dir temp
    > ...
    >
    > 2005-11-12 00:00 <KAT> .
    > 2005-11-12 00:00 <KAT> ..
    > 2005-11-12 00:00 20 bacon.dat
    > 2005-11-12 00:00 <KAT> egg
    > 2005-11-12 00:00 20 spam.txt
    > 2 fil(er) 40 byte
    > 3 katalog(er) 9 818 021 888 byte ledigt
    >
    > C:\>python script.py
    > c:\TEMP\bacon.dat
    > c:\TEMP\egg
    > c:\TEMP\spam.txt
    > ['c:\\TEMP\\egg']
    >
    > </F>
    >
    >
    >
    Michael Konrad, Nov 12, 2005
    #17
  18. Sorry about that, I guess send was working.

    Michael Konrad wrote:
    >
    > This is what I decided on for a solution. I haven't tested it
    > cross-platform yet.
    >
    > import os
    >
    > def dirListing(directory='/Users/mkonrad'):
    > """Returns a list of directories."""
    > #variables
    > dirs = [] #list of directories
    >
    > #list of directories and files
    > listing = os.listdir(directory)
    >
    > #get just the directories
    > for x in listing:
    > if os.path.isdir(directory+os.sep+x):
    > dirs.append(x)
    >
    > return dirs
    >
    > Fredrik Lundh wrote:
    >> "Shi Mu" wrote:
    >>
    >>> print buildList() gets lots of stuffs from my temp directory(there do
    >>> exist lots of files).
    >>> But why "print x' has nothing?

    >>
    >> C:\>more script.py
    >> import os
    >>
    >> def buildList( directory='c:\TEMP' ):
    >> dirs = [ ]
    >> listing = os.listdir(directory)
    >> for x in listing:
    >> x = os.path.join(directory, x)
    >> print x
    >> if os.path.isdir(x):
    >> dirs.append(x)
    >> return dirs
    >>
    >> print buildList()
    >>
    >> C:\>dir temp
    >> ...
    >>
    >> 2005-11-12 00:00 <KAT> .
    >> 2005-11-12 00:00 <KAT> ..
    >> 2005-11-12 00:00 20 bacon.dat
    >> 2005-11-12 00:00 <KAT> egg
    >> 2005-11-12 00:00 20 spam.txt
    >> 2 fil(er) 40 byte
    >> 3 katalog(er) 9 818 021 888 byte ledigt
    >>
    >> C:\>python script.py
    >> c:\TEMP\bacon.dat
    >> c:\TEMP\egg
    >> c:\TEMP\spam.txt
    >> ['c:\\TEMP\\egg']
    >>
    >> </F>
    >>
    >>
    >>

    >
    Michael Konrad, Nov 12, 2005
    #18
  19. SU News Server

    Guest

    Shi Mu:

    Before all you were doing was defining a function with:
    import os

    def buildList( directory='c:\TEMP' ):
    dirs = [ ]
    listing = os.listdir(directory)
    for x in listing:
    x = os.path.join(directory, x)
    print x
    if os.path.isdir(x):
    dirs.append(x)
    return dirs

    when you python this file, it does not execute the function, it only
    defines it.
    Later Lundh told you to add:
    print buildList()

    to the end of the file. Not only does this execute buildList() but it
    also prints out the list "dirs" that buildList returns. So the first
    time it wasn't that "print x" wasn't printing anything, it was only
    that you weren't executing the function buildList(). If, at the end of
    the file, you put buildList() you will only see output values
    corresponding to the print x statement
    , Nov 12, 2005
    #19
  20. SU News Server

    Shi Mu Guest

    thanks a lot!

    On 11/11/05, Michael Konrad <> wrote:
    >
    > This is what I decided on for a solution. I haven't tested it
    > cross-platform yet.
    >
    > import os
    >
    > def dirListing(directory='/Users/mkonrad'):
    > """Returns a list of directories."""
    > #variables
    > dirs = [] #list of directories
    >
    > #list of directories and files
    > listing = os.listdir(directory)
    >
    > #get just the directories
    > for x in listing:
    > if os.path.isdir(directory+os.sep+x):
    > dirs.append(x)
    >
    > return dirs
    >
    > Fredrik Lundh wrote:
    > > "Shi Mu" wrote:
    > >
    > >> print buildList() gets lots of stuffs from my temp directory(there do
    > >> exist lots of files).
    > >> But why "print x' has nothing?

    > >
    > > C:\>more script.py
    > > import os
    > >
    > > def buildList( directory='c:\TEMP' ):
    > > dirs = [ ]
    > > listing = os.listdir(directory)
    > > for x in listing:
    > > x = os.path.join(directory, x)
    > > print x
    > > if os.path.isdir(x):
    > > dirs.append(x)
    > > return dirs
    > >
    > > print buildList()
    > >
    > > C:\>dir temp
    > > ...
    > >
    > > 2005-11-12 00:00 <KAT> .
    > > 2005-11-12 00:00 <KAT> ..
    > > 2005-11-12 00:00 20 bacon.dat
    > > 2005-11-12 00:00 <KAT> egg
    > > 2005-11-12 00:00 20 spam.txt
    > > 2 fil(er) 40 byte
    > > 3 katalog(er) 9 818 021 888 byte ledigt
    > >
    > > C:\>python script.py
    > > c:\TEMP\bacon.dat
    > > c:\TEMP\egg
    > > c:\TEMP\spam.txt
    > > ['c:\\TEMP\\egg']
    > >
    > > </F>
    > >
    > >
    > >

    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Shi Mu, Nov 13, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JD

    Directory Listing

    JD, Aug 24, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,125
  2. Todd Lu

    Directory listing

    Todd Lu, Aug 31, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    452
    Todd Lu
    Aug 31, 2004
  3. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    directory listing details

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, May 24, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    395
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    May 24, 2005
  4. Tim_Mac
    Replies:
    2
    Views:
    360
    Tim_Mac
    Dec 15, 2005
  5. Light
    Replies:
    1
    Views:
    928
    Eliyahu Goldin
    Mar 22, 2006
Loading...

Share This Page