Curious issue with simple code

C

codefire

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')

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
 
D

Diez B. Roggisch

codefire said:
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')

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
 
F

Fredrik Lundh

codefire said:
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>
 
M

MonkeeSage

codefire said:
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
 
T

Tim Chase

Code:
[QUOTE]
> 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')
 >
>
> 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,[/QUOTE]

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,
'/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
 
C

codefire

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
 
G

George Sakkis

codefire said:
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
 
E

eldorado

Hello,

I am trying to parse some files so that if a postal code exists, but is
longer than five digits it will return me only the first five digits:
....
for insDict in insureDict:
insDict['postalcode'] = insDict.get('postalcode')[:5]
....
This works, except for when I get a blank postalcode. In which case I get
the following error.
ERR exceptions.TypeError: iteration over non-sequence

What I would like to do is to check to make sure a value exists. I just
cannot seem to find the syntax to do that. (not asking anyone to write my
code, just looking for a pointer)

Thanks
 
G

George Sakkis

eldorado said:
Hello,

I am trying to parse some files so that if a postal code exists, but is
longer than five digits it will return me only the first five digits:
...
for insDict in insureDict:
insDict['postalcode'] = insDict.get('postalcode')[:5]
...
This works, except for when I get a blank postalcode. In which case I get
the following error.
ERR exceptions.TypeError: iteration over non-sequence

What I would like to do is to check to make sure a value exists. I just
cannot seem to find the syntax to do that. (not asking anyone to write my
code, just looking for a pointer)

Thanks

Hint: dict.get() takes an optional second argument.

George

PS: To make a new thread, don't hit "reply" on an existing thread and
change the subject. Just make a new thread.. duh.
 
C

codefire

George said:
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

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
 
J

John Machin

eldorado said:
Hello,

I am trying to parse some files so that if a postal code exists, but is
longer than five digits it will return me only the first five digits:
...
for insDict in insureDict:
insDict['postalcode'] = insDict.get('postalcode')[:5]
...
This works, except for when I get a blank postalcode. In which case I get (> the following error.
ERR exceptions.TypeError: iteration over non-sequence

What is "blank"? Do you mean "empty" as in zero-length, or "contains
one or more spaces" as in " " or do you mean "is None"? Which
statement causes the error? Could you possibly provide the traceback?

If an insDict has no key 'postalcode', then the .get() will return
None, and that insDict will have the equivalent of:
insDict['postalcode'] = None
done to it, which may not be what you want.

However this will not of itself cause "TypeError: iteration over
non-sequence" ... either

(1) insureDict is not iterable -- BTW insureDict is a strangely chosen
name; it implies it is a dictionary, BUT insDict is quite obviously a
dict, which can't be used as a key for another dict .... what is
type(insureDict)?

or (2) you are getting this error later by trying to iterate over the
values in insDict dicts -- you think the values are all strings but one
or more contain None ... show us the traceback!!
What I would like to do is to check to make sure a value exists.

Such a *what* value exists *where*?

HTH,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top