stupid question about os.listdir

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason Kratz

    stupid question about os.listdir

    OK. I've search on google groups and around the web for this and I
    haven't found an answer. I'm a Python newbie and have what I assume is
    a basic question. os.listdir takes a pathname as an arg but it doesn't
    actually list the contents of the dir I pass in. it always operates on
    the current dir (wherever the script is run) and I have to chdir
    beforehand. Is that how its supposed to work? If so what is the point
    in passing in a pathname?

    thanks,

    Jason

  • Jason Kratz

    #2
    Re: stupid question about os.listdir

    Jason Kratz wrote:[color=blue]
    > OK. I've search on google groups and around the web for this and I
    > haven't found an answer. I'm a Python newbie and have what I assume is
    > a basic question. os.listdir takes a pathname as an arg but it doesn't
    > actually list the contents of the dir I pass in. it always operates on
    > the current dir (wherever the script is run) and I have to chdir
    > beforehand. Is that how its supposed to work? If so what is the point
    > in passing in a pathname?
    >
    > thanks,
    >
    > Jason
    >[/color]

    oops. almost forgot. if I run interactively in the python interpreter
    it works as I expect. its when doing 'python script.py' from the
    command line that it only uses the current directory.

    Comment

    • Erik Max Francis

      #3
      Re: stupid question about os.listdir

      Jason Kratz wrote:
      [color=blue]
      > oops. almost forgot. if I run interactively in the python
      > interpreter
      > it works as I expect. its when doing 'python script.py' from the
      > command line that it only uses the current directory.[/color]

      Never heard of any such thing. It's likely that your script is not
      quite doing what you expect it to.

      --
      Erik Max Francis && [email protected] && http://www.alcyone.com/max/
      __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
      / \ Heaven and Hell / Is on Earth
      \__/ Salt-n-Pepa

      Comment

      • Ben Finney

        #4
        Re: stupid question about os.listdir

        On Fri, 27 Jun 2003 02:22:36 GMT, Jason Kratz wrote:[color=blue]
        > os.listdir takes a pathname as an arg but it doesn't actually list the
        > contents of the dir I pass in.[/color]

        Please reduce the problem to a simple script that others can examine,
        and post it here. If the behaviour is as you say, it should be only a
        few lines long:

        import os
        os.listdir( 'somethingyouth inkshouldwork' )

        --
        \ "God forbid that any book should be banned. The practice is as |
        `\ indefensible as infanticide." -- Dame Rebecca West |
        _o__) |
        http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B

        Comment

        • Jason Kratz

          #5
          Re: stupid question about os.listdir

          Here is more clarification. Here is my script called backup.py

          import os.path
          import os

          def getdirs(path):

          dirs = []

          os.chdir(path)
          for entry in os.listdir(path ):
          if os.path.isdir(e ntry):
          dirs.append(ent ry)

          return dirs

          print getdirs('/')


          if I run this from the command line on linux with 'python backup.py' it
          works *if* I have os.chdir in there. if I comment it out it doesnt list
          starting from the root dir...it starts in my home dir.

          If go into the interactive command mode by just typing 'python' at the
          prompt and do:

          import os
          os.listdir('/') then it prints out the dirs under root.

          incidentally this happens on windows as well

          Jason Kratz wrote:[color=blue]
          > OK. I've search on google groups and around the web for this and I
          > haven't found an answer. I'm a Python newbie and have what I assume is
          > a basic question. os.listdir takes a pathname as an arg but it doesn't
          > actually list the contents of the dir I pass in. it always operates on
          > the current dir (wherever the script is run) and I have to chdir
          > beforehand. Is that how its supposed to work? If so what is the point
          > in passing in a pathname?
          >
          > thanks,
          >
          > Jason
          >[/color]

          Comment

          • John Hunter

            #6
            Re: stupid question about os.listdir

            >>>>> "Jason" == Jason Kratz <[email protected] > writes:

            Jason> oops. almost forgot. if I run interactively in the python
            Jason> interpreter it works as I expect. its when doing 'python
            Jason> script.py' from the command line that it only uses the
            Jason> current directory.

            Code, we need more code. Please post an example, your platform, and
            python version.

            The following works for me on linux w/ python2.2 called as[color=blue]
            > python scriptname.py[/color]

            import os
            print os.listdir('/home/jdhunter')
            print os.listdir('/home/jdhunter/python')

            and the same script (w/ different test paths) works on win32 w/ python
            2.2.

            JDH

            Comment

            • Ben Finney

              #7
              Re: stupid question about os.listdir

              On Fri, 27 Jun 2003 02:57:33 GMT, Jason Kratz wrote:[color=blue]
              > Here is more clarification. Here is my script called backup.py[/color]

              Again, I'll ask you to reduce this to a single, isolated incident of
              os.listdir() that doesn't act as you expect.

              If the failure *depends* on the rest of the script, then it's more
              complex than "os.listdir () doesn't list the current directory". It may,
              in fact, have nothing to do with os.listdir() at all.

              Reducing the test case to a single os.listdir() instance will aid you as
              well as us, since you'll be able to have a much better understanding of
              what's going on.

              --
              \ "I was sad because I had no shoes, until I met a man who had no |
              `\ feet. So I said, 'Got any shoes you're not using?'" -- Steven |
              _o__) Wright |
              http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B

              Comment

              • Jason Kratz

                #8
                Re: stupid question about os.listdir

                Ben Finney wrote:[color=blue]
                > On Fri, 27 Jun 2003 02:22:36 GMT, Jason Kratz wrote:
                >[color=green]
                >>os.listdir takes a pathname as an arg but it doesn't actually list the
                >>contents of the dir I pass in.[/color]
                >
                >
                > Please reduce the problem to a simple script that others can examine,
                > and post it here. If the behaviour is as you say, it should be only a
                > few lines long:
                >
                > import os
                > os.listdir( 'somethingyouth inkshouldwork' )
                >[/color]

                Ben...I tried the above in a new script file (with print os.listdir) and
                it works as I thought my other should. Which means i'm doing something
                wrong when passing the path in to my function but I'm not sure what. ugh.

                Comment

                • Jason Kratz

                  #9
                  Re: stupid question about os.listdir

                  Ben Finney wrote:[color=blue]
                  > On Fri, 27 Jun 2003 03:06:04 GMT, Jason Kratz wrote:
                  >[color=green]
                  >>Ben Finney wrote:
                  >>[color=darkred]
                  >>>Please reduce the problem to a simple script[/color]
                  >>
                  >>Ben...I tried the above in a new script file (with print os.listdir)
                  >>and it works as I thought my other should. Which means i'm doing
                  >>something wrong when passing the path in to my function but I'm not
                  >>sure what. ugh.[/color]
                  >
                  >
                  > Congratulations ! You've learned an immensely valuable debugging
                  > technique: Reduce the problem behaviour to the *minimum necessary code*
                  > to reproduce the problem; otherwise, you're searhing in code that, it
                  > turns out, has absolutely no bearing on the problem.
                  >
                  > (This leads, in turn, to the principle that writing less code in the
                  > first place leads to fewer bugs -- but that will come naturally as you
                  > learn Python :-)
                  >[/color]

                  aha! I found it! its the call to os.path.isdir. I'm not passing it
                  a real pathname....jus t a string. I need to set my entries in my dir
                  list as real pathnames (ie: with the slashes)...not just the text.
                  question is how ;)

                  Comment

                  • Peter Hansen

                    #10
                    Re: stupid question about os.listdir

                    Jason Kratz wrote:[color=blue]
                    >
                    > def getdirs(path):
                    > os.chdir(path)
                    > for entry in os.listdir(path ):
                    > if os.path.isdir(e ntry):
                    > dirs.append(ent ry)
                    >
                    > if I run this from the command line on linux with 'python backup.py' it
                    > works *if* I have os.chdir in there. if I comment it out it doesnt list
                    > starting from the root dir...it starts in my home dir.[/color]

                    This might mean you are not passing it an absolute path, but
                    instead a relative one. Absolute paths (on Linux) are those
                    which start with a / (forward slash). Anything without that
                    will start from the current directory only.

                    But without actual examples of which paths are failing, as
                    Ben has asked for, we know nothing for certain.

                    Is it also possible that you are not having a problem with listdir()
                    at all, but with the values you are passing to os.path.isdir() ?
                    You realize that os.listdir() returns names that are *relative*
                    to the path parameter you give it? So that if you then pass those
                    to isdir() you will get nothing useful if you don't first do
                    this instead? :

                    entry = os.path.join(pa th, entry)
                    if os.path.isdir(e ntry):
                    dirs.append(ent ry)

                    -Peter

                    Comment

                    • Erik Max Francis

                      #11
                      Re: stupid question about os.listdir

                      Jason Kratz wrote:
                      [color=blue]
                      > aha! I found it! its the call to os.path.isdir. I'm not passing
                      > it
                      > a real pathname....jus t a string. I need to set my entries in my dir
                      > list as real pathnames (ie: with the slashes)...not just the text.
                      > question is how ;)[/color]

                      A pathname _is_ just a string. Presumably the problem is that you're
                      getting the results of os.listdir, which are just the names of the
                      contents of the directory, not the relative paths to them:

                      max@oxygen:~/tmp% mkdir subdir
                      max@oxygen:~/tmp% touch subdir/a subdir/b subdir/c
                      max@oxygen:~/tmp% ls subdir
                      a b c
                      max@oxygen:~/tmp% python
                      Python 2.2.3 (#1, May 31 2003, 21:31:33)
                      [GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
                      Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
                      >>> import os
                      >>> os.listdir('sub dir')[/color][/color][/color]
                      ['a', 'b', 'c']

                      So just join the paths:
                      [color=blue][color=green][color=darkred]
                      >>> for f in os.listdir('sub dir'):[/color][/color][/color]
                      .... p = os.path.join('s ubdir', f)
                      .... print p, os.path.isfile( p)
                      ....
                      subdir/a 1
                      subdir/b 1
                      subdir/c 1

                      --
                      Erik Max Francis && [email protected] && http://www.alcyone.com/max/
                      __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
                      / \ ike anybody, I would like to live a long life.
                      \__/ Dr. Martin Luther King, Jr.

                      Comment

                      • Jason Kratz

                        #12
                        Re: stupid question about os.listdir

                        Peter Hansen wrote:[color=blue]
                        > Jason Kratz wrote:
                        >[color=green]
                        >>def getdirs(path):
                        >> os.chdir(path)
                        >> for entry in os.listdir(path ):
                        >> if os.path.isdir(e ntry):
                        >> dirs.append(ent ry)
                        >>
                        >>if I run this from the command line on linux with 'python backup.py' it
                        >>works *if* I have os.chdir in there. if I comment it out it doesnt list
                        >>starting from the root dir...it starts in my home dir.[/color]
                        >
                        >
                        > This might mean you are not passing it an absolute path, but
                        > instead a relative one. Absolute paths (on Linux) are those
                        > which start with a / (forward slash). Anything without that
                        > will start from the current directory only.
                        >
                        > But without actual examples of which paths are failing, as
                        > Ben has asked for, we know nothing for certain.
                        >
                        > Is it also possible that you are not having a problem with listdir()
                        > at all, but with the values you are passing to os.path.isdir() ?
                        > You realize that os.listdir() returns names that are *relative*
                        > to the path parameter you give it? So that if you then pass those
                        > to isdir() you will get nothing useful if you don't first do
                        > this instead? :
                        >
                        > entry = os.path.join(pa th, entry)
                        > if os.path.isdir(e ntry):
                        > dirs.append(ent ry)
                        >
                        > -Peter[/color]

                        Peter-

                        Thanks much for the reply. You are completely correct and I managed to
                        work thru it myself (just finished right before I read your posting) and
                        I actually had the exact code (aside from using a new var instead of
                        reusing entry) you have just above (with the join). Was going to post
                        it and find out if that was the "correct" way of doing it ;)

                        This is my code:

                        import os
                        import os.path

                        def getdirs(path):
                        dirs=[]

                        for entry in os.listdir(path ):
                        fullpath=os.pat h.join(path,ent ry)
                        if os.path.isdir(f ullpath):
                        dirs.append(ful lpath)
                        return dirs

                        print getdirs('/')

                        Comment

                        Working...