Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » Python

Code works in current dir only?

 
Jump to:  
 
Paxton Sanders
PostPosted: Fri Aug 15, 2008 7:47 pm    Post subject: 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(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("/")


--
Paxton Sanders
pcsanders@yahoo.com
 

 
Dieter Deyke
PostPosted: Fri Aug 15, 2008 8:05 pm    Post subject: Re: Code works in current dir only?
       
Paxton Sanders <pcsanders@yahoo.com> writes:

Quote:
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
 

 
Paxton Sanders
PostPosted: Fri Aug 15, 2008 8:09 pm    Post subject: Re: Code works in current dir only?
       
Quote:
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! :)

--
Paxton Sanders
pcsanders@yahoo.com
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

odżywki fajerwerki lektury Kochanowski Jan wiersze mieszkania łódź