codefire wrote:
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?
try printing both f and fp, and see if you can tell
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
codefire wrote:
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
codefire wrote:
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?
Hi Tony,
Actually the file is in a different directory
[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
Ah of course, isfile(f) can only return true if it can find f! :)
I'm going to investigate those other functions too :)
Thanks a lot guys!
Tony
--
http://mail.python.org/mailman/listinfo/python-list
codefire wrote:
Ah of course, isfile(f) can only return true if it can find f! :)
I'm going to investigate those other functions too :)
Thanks a lot guys!
Tony
By the way, an easier way to deal with paths is the path.py module
(http://www.jorendorff.com/articles/python/path/). Your example
George Sakkis wrote:
By the way, an easier way to deal with paths is the path.py module
(http://www.jorendorff.com/articles/python/path/). Your example could
be rewritten simply as:
from path import path
for html_file in path(start_dir).walkfiles('*.html'):
print 'html file found!'