Code works in current dir only?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paxton Sanders

    Code works in current dir only?

    Does anyone know why the following code successfully labels (with [f|d|?])
    entries in the current directory, but not in any other
    directory? (All other directories' entries print [?].)

    I'm using Python 2.5.1 on Cygwin.

    Thanks!

    import os

    # collect all files and their paths
    def collectinfo(pat h):
    files = os.listdir(path )
    files.sort()
    for n in files:
    if os.path.isdir(n ):
    print "[d]", n
    elif os.path.isfile( n):
    print "[f]", n
    else:
    print "[?]", n

    # this works
    if __name__ == "__main__":
    collectinfo("." )

    # this does not work, always labels with [?]
    #if __name__ == "__main__":
    # collectinfo("/")


    --
    Paxton Sanders
    pcsanders@yahoo .com
  • Dieter Deyke

    #2
    Re: Code works in current dir only?

    Paxton Sanders <pcsanders@yaho o.comwrites:
    Does anyone know why the following code successfully labels (with [f|d|?])
    entries in the current directory, but not in any other
    directory? (All other directories' entries print [?].)
    >
    I'm using Python 2.5.1 on Cygwin.
    >
    Thanks!
    >
    import os
    >
    # collect all files and their paths
    def collectinfo(pat h):
    files = os.listdir(path )
    files.sort()
    for n in files:
    n = os.path.join(pa th, n) ### <== you need this
    if os.path.isdir(n ):
    print "[d]", n
    elif os.path.isfile( n):
    print "[f]", n
    else:
    print "[?]", n
    >
    # this works
    if __name__ == "__main__":
    collectinfo("." )
    >
    # this does not work, always labels with [?]
    #if __name__ == "__main__":
    # collectinfo("/")
    --
    Dieter Deyke

    Comment

    • Paxton Sanders

      #3
      Re: Code works in current dir only?

      >Does anyone know why the following code successfully labels (with
      >[f|d|?]) entries in the current directory, but not in any other
      >directory? (All other directories' entries print [?].)
      n = os.path.join(pa th, n) ### <== you need this

      Oops, there it is: my stupidity on display forever.

      Thanks for the catch! :)

      --
      Paxton Sanders
      pcsanders@yahoo .com

      Comment

      Working...