Curious issue with simple code

Diez B. Roggisch deets at
Tue Sep 19 19:27:36 CEST 2006

codefire wrote:

> Hi,
> I have some simple code - which works...kind'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?

Of course not, unless a file with that very name exists in the current
working directory. Otherwise, where would be the difference between a
full-path and the leaf path?


More information about the Python-list mailing list