Code works in current dir only?

P

Paxton Sanders

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:
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("/")
 
D

Dieter Deyke

Paxton Sanders said:
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("/")
 
P

Paxton Sanders

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(path, n) ### <== you need this


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

Thanks for the catch! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top