Curious issue with simple code

Richard Morello richard at sneakerfish.com
Tue Sep 19 13:29:23 EDT 2006


Dear Tony,

You're not in that directory (start_dir) when the isfile() function is
called.  See function os.path.curdir() and os.chdir().  Also, you may be
confusing the behavior of os.path.walk(), in which the function called will
happen once you have been chdired to the directory it is examining.  

Hope this was helpful.

Yours truly,
Rich.

-----Original Message-----
From: python-list-bounces+richard=sneakerfish.com at python.org
[mailto:python-list-bounces+richard=sneakerfish.com at python.org] On Behalf Of
codefire
Sent: Tuesday, September 19, 2006 1:08 PM
To: python-list at python.org
Subject: Curious issue with simple code


Hi,

I have some simple code - which works...kind of..here's the code:

[code]
import os

def print_tree(start_dir):
    for f in os.listdir(start_dir):
        fp = os.path.join(start_dir, f)
        print fp
        if os.path.isfile(fp): # will return false if use f here!
            if os.path.splitext(fp)[1] == '.html':
                print 'html file found!'
        if os.path.isdir(fp):
            print_tree(fp)

print os.path
print_tree(r'c:\intent\docn')
[/code]

As above it all works as expected. However, on the marked line, if I use f
instead of fp then that condition returns false! Surely,
isfile(f) should return true, even if I just give a filename, rather than
the full path? 

If anyway can explain this I'd be grateful,

Tony

-- 
http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list