Re: Curious issue with simple code
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 the difference between the two filenames... /F -- http://mail.python.org/mailman/listinfo/python-list
RE: Curious issue with simple code
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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of codefire Sent: Tuesday, September 19, 2006 1:08 PM To: python-list@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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Curious issue with simple code
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 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? Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Curious issue with simple code
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 from the working directory, so you need to give it the path where to find it. If you started the script from 'c:\intent\docn' (i.e., start_dir), then just using f would work. Regards, Jordan -- http://mail.python.org/mailman/listinfo/python-list
Re: Curious issue with simple 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, If your current working directory (CWD) is the same as start_dir, the behaviors of using f and fp will be the same. However, if your CWD is *not* the same, f is relative to the CWD, and fp is start_dir + f relative to the CWD. Thus, start_dir = 'temp' os.path.abspath(os.path.curdir) '/home/tim' f = 'filename' fp = os.path.join(start_dir, f) fp 'temp/filename' os.path.abspath(f) '/home/tim/filename' os.path.abspath(fp) '/home/tim/temp/filename' You may also want to read up on os.walk: for root, dirs, files in os.walk(start_dir): for f in files: if os.path.splitext(f)[1].lower()[1:] == 'html': print 'HTML:', os.path.join(root, f) #else: #print 'Not HTML:', os.path.join(root, f) which allows you to easily do what it looks like your code is doing. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Curious issue with simple code
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
Re: Curious issue with simple code
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 could be rewritten simply as: from path import path for html_file in path(start_dir).walkfiles('*.html'): print 'html file found!' George -- http://mail.python.org/mailman/listinfo/python-list
Re: Curious issue with simple code
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!' Thanks George, I had played with it some more and ended up with the fairly succinct: [code] import os import glob for f in glob.glob('c:\\intent\\docn\\*\\*.html'): print f [/code] It works quite nicely - I'm just twiddling - first time writing Python (but like it) :) I've bookmarked that site too for future ref. Thanks again, Tony -- http://mail.python.org/mailman/listinfo/python-list