directory listing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Konrad

    #16
    Re: directory listing


    This is what I decided on for a solution. I haven't tested it
    cross-platform yet.

    import os

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

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

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

    return dirs

    Fredrik Lundh wrote:[color=blue]
    > "Shi Mu" wrote:
    >[color=green]
    >> print buildList() gets lots of stuffs from my temp directory(there do
    >> exist lots of files).
    >> But why "print x' has nothing?[/color]
    >
    > C:\>more script.py
    > import os
    >
    > def buildList( directory='c:\T EMP' ):
    > dirs = [ ]
    > listing = os.listdir(dire ctory)
    > for x in listing:
    > x = os.path.join(di rectory, 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.d at
    > c:\TEMP\egg
    > c:\TEMP\spam.tx t
    > ['c:\\TEMP\\egg']
    >
    > </F>
    >
    >
    >[/color]


    Comment

    • Michael Konrad

      #17
      Re: directory listing


      This is what I decided on for a solution. I haven't tested it
      cross-platform yet.

      import os

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

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

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

      return dirs

      Fredrik Lundh wrote:[color=blue]
      > "Shi Mu" wrote:
      >[color=green]
      >> print buildList() gets lots of stuffs from my temp directory(there do
      >> exist lots of files).
      >> But why "print x' has nothing?[/color]
      >
      > C:\>more script.py
      > import os
      >
      > def buildList( directory='c:\T EMP' ):
      > dirs = [ ]
      > listing = os.listdir(dire ctory)
      > for x in listing:
      > x = os.path.join(di rectory, 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.d at
      > c:\TEMP\egg
      > c:\TEMP\spam.tx t
      > ['c:\\TEMP\\egg']
      >
      > </F>
      >
      >
      >[/color]

      Comment

      • Michael Konrad

        #18
        Re: directory listing

        Sorry about that, I guess send was working.

        Michael Konrad wrote:[color=blue]
        >
        > This is what I decided on for a solution. I haven't tested it
        > cross-platform yet.
        >
        > import os
        >
        > def dirListing(dire ctory='/Users/mkonrad'):
        > """Returns a list of directories."""
        > #variables
        > dirs = [] #list of directories
        >
        > #list of directories and files
        > listing = os.listdir(dire ctory)
        >
        > #get just the directories
        > for x in listing:
        > if os.path.isdir(d irectory+os.sep +x):
        > dirs.append(x)
        >
        > return dirs
        >
        > Fredrik Lundh wrote:[color=green]
        >> "Shi Mu" wrote:
        >>[color=darkred]
        >>> print buildList() gets lots of stuffs from my temp directory(there do
        >>> exist lots of files).
        >>> But why "print x' has nothing?[/color]
        >>
        >> C:\>more script.py
        >> import os
        >>
        >> def buildList( directory='c:\T EMP' ):
        >> dirs = [ ]
        >> listing = os.listdir(dire ctory)
        >> for x in listing:
        >> x = os.path.join(di rectory, 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.d at
        >> c:\TEMP\egg
        >> c:\TEMP\spam.tx t
        >> ['c:\\TEMP\\egg']
        >>
        >> </F>
        >>
        >>
        >>[/color]
        >[/color]

        Comment

        • jay.dow@gmail.com

          #19
          Re: directory listing

          Shi Mu:

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

          def buildList( directory='c:\T EMP' ):
          dirs = [ ]
          listing = os.listdir(dire ctory)
          for x in listing:
          x = os.path.join(di rectory, 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

          Comment

          • Shi Mu

            #20
            Re: directory listing

            thanks a lot!

            On 11/11/05, Michael Konrad <mkonrad@syr.ed u> wrote:[color=blue]
            >
            > This is what I decided on for a solution. I haven't tested it
            > cross-platform yet.
            >
            > import os
            >
            > def dirListing(dire ctory='/Users/mkonrad'):
            > """Returns a list of directories."""
            > #variables
            > dirs = [] #list of directories
            >
            > #list of directories and files
            > listing = os.listdir(dire ctory)
            >
            > #get just the directories
            > for x in listing:
            > if os.path.isdir(d irectory+os.sep +x):
            > dirs.append(x)
            >
            > return dirs
            >
            > Fredrik Lundh wrote:[color=green]
            > > "Shi Mu" wrote:
            > >[color=darkred]
            > >> print buildList() gets lots of stuffs from my temp directory(there do
            > >> exist lots of files).
            > >> But why "print x' has nothing?[/color]
            > >
            > > C:\>more script.py
            > > import os
            > >
            > > def buildList( directory='c:\T EMP' ):
            > > dirs = [ ]
            > > listing = os.listdir(dire ctory)
            > > for x in listing:
            > > x = os.path.join(di rectory, 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.d at
            > > c:\TEMP\egg
            > > c:\TEMP\spam.tx t
            > > ['c:\\TEMP\\egg']
            > >
            > > </F>
            > >
            > >
            > >[/color]
            >
            > --
            > http://mail.python.org/mailman/listinfo/python-list
            >[/color]

            Comment

            Working...