directory listing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SU News Server

    #1

    directory listing

    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(dire ctory)

    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.ge tcwd()
    Then it returns a list of directories.

    Any ideas?

    Thank you,
    -Michael




  • Fredrik Lundh

    #2
    Re: directory listing

    "SU News Server" <mkonrad@syr.ed u> wrote:
    [color=blue]
    > 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(dire ctory)
    >
    > 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.ge tcwd()
    > Then it returns a list of directories.[/color]

    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(di rectory, x)
    if os.path.isdir(x ):
    dirs.append(x)

    or use the glob module instead:

    listing = glob.glob(os.pa th.join(directo ry, "*"))

    hope this helps!

    </F>



    Comment

    • Richard Townsend

      #3
      Re: directory listing

      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(di rectory, x)


      --
      Richard

      Comment

      • Michael Konrad

        #4
        Re: directory listing

        "Fredrik Lundh" <fredrik@python ware.com> wrote:
        [color=blue]
        > "SU News Server" <mkonrad@syr.ed u> wrote:
        >[color=green]
        >> 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(dire ctory)
        >>
        >> 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.ge tcwd()
        >> Then it returns a list of directories.[/color]
        >
        > 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)
        >[/color]

        Did that and I was just getting a bunch of [ ].
        [color=blue]
        > (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(di rectory, x)
        > if os.path.isdir(x ):
        > dirs.append(x)[/color]

        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.
        [color=blue]
        >
        > or use the glob module instead:
        >
        > listing = glob.glob(os.pa th.join(directo ry, "*"))
        >
        > hope this helps!
        >
        > </F>
        >
        >
        >
        >[/color]

        Comment

        • Michael Konrad

          #5
          Re: directory listing

          Richard Townsend <richard@nospam .com> wrote:
          [color=blue]
          > 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(di rectory, x)
          >
          >
          >[/color]

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

          Thank you for responding.
          _Michael


          Comment

          • Richard Townsend

            #6
            Re: directory listing

            On 11 Nov 2005 22:00:04 GMT, Michael Konrad wrote:
            [color=blue]
            > Richard Townsend <richard@nospam .com> wrote:
            >[color=green]
            >> 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(di rectory, x)
            >>
            >>
            >>[/color]
            >
            > I wonder if I can join ./, so I don't have the full path in each
            > entry?
            >
            > Thank you for responding.
            > _Michael[/color]

            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

            Comment

            • Fredrik Lundh

              #7
              Re: directory listing

              Michael Konrad wrote:
              [color=blue][color=green]
              > > for x in listing:
              > > print x
              > > if os.path.isdir(x ):
              > > dirs.append(x)
              > >[/color]
              >
              > Did that and I was just getting a bunch of [ ].[/color]

              if you printed "x" (the filename), that doesn't sound very likely.
              maybe you printed some other variable? (like "dirs")
              [color=blue][color=green]
              > > for x in listing:
              > > x = os.path.join(di rectory, x)
              > > if os.path.isdir(x ):
              > > dirs.append(x)[/color]
              >
              > OK. This works except each entry in dirs now includes the full path.
              > Is there an unjoin? :)[/color]

              use two variables:

              for name in listing:
              fullname = os.path.join(di rectory, name)
              if os.path.isdir(f ullname):
              dirs.append(nam e)

              </F>



              Comment

              • Shi Mu

                #8
                Re: directory listing

                On 11 Nov 2005 22:00:04 GMT, Michael Konrad <mkonrad@syr.ed u> wrote:[color=blue]
                > Richard Townsend <richard@nospam .com> wrote:
                >[color=green]
                > > 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(di rectory, x)
                > >
                > >
                > >[/color]
                >
                > 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
                >[/color]
                I tried this and no error reported but nothing appear on the console, why?

                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

                Comment

                • Fredrik Lundh

                  #9
                  Re: directory listing

                  Shi Mu wrote:
                  [color=blue]
                  > I tried this and no error reported but nothing appear on the console, why?
                  >
                  > 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[/color]

                  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>



                  Comment

                  • Shi Mu

                    #10
                    Re: directory listing

                    On 11/11/05, Fredrik Lundh <fredrik@python ware.com> wrote:[color=blue]
                    > Shi Mu wrote:
                    >[color=green]
                    > > I tried this and no error reported but nothing appear on the console, why?
                    > >
                    > > 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[/color]
                    >
                    > 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.[/color]

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

                    Comment

                    • Fredrik Lundh

                      #11
                      Re: directory listing

                      "Shi Mu" wrote:
                      [color=blue]
                      > but i am curious why the line of "print x" does not show
                      > anything.[/color]

                      because your c:\temp directory is empty ?

                      </F>



                      Comment

                      • Shi Mu

                        #12
                        Re: directory listing

                        On 11/11/05, Fredrik Lundh <fredrik@python ware.com> wrote:[color=blue]
                        > "Shi Mu" wrote:
                        >[color=green]
                        > > but i am curious why the line of "print x" does not show
                        > > anything.[/color]
                        >
                        > because your c:\temp directory is empty ?
                        >
                        > </F>[/color]
                        print buildList() gets lots of stuffs from my temp directory(there do
                        exist lots of files).
                        But why "print x' has nothing?

                        Comment

                        • Fredrik Lundh

                          #13
                          Re: directory listing

                          "Shi Mu" wrote:
                          [color=blue]
                          > 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>



                          Comment

                          • Peter Hansen

                            #14
                            Re: directory listing

                            Shi Mu wrote:[color=blue]
                            > On 11/11/05, Fredrik Lundh <fredrik@python ware.com> wrote:[color=green]
                            >>Shi Mu wrote:[color=darkred]
                            >>>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[/color][/color]
                            >
                            > It works but i am curious why the line of "print x" does not show
                            > anything. many thanks![/color]

                            Did you use directory='c:\T EMP' as shown above, or directory='c:\t emp' ?
                            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

                            Comment

                            • Michael Konrad

                              #15
                              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

                              Working...