Code works in current dir only?

Dieter Deyke ddeyke at ptc.com
Fri Aug 15 18:05:13 EDT 2008


Paxton Sanders <pcsanders at yahoo.com> writes:

> 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(path):
>     files = os.listdir(path)
>     files.sort()
>     for n in files:
          n = os.path.join(path, 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



More information about the Python-list mailing list