isfile . is dir

Mark McEahern marklists at mceahern.com
Wed Feb 19 15:05:33 EST 2003


> I'm testing wether or not files in listdir() are files or dirs.
> What am I doing that all jar files are not recognized as files?
>
> for x in os.listdir(SRCDIR +'/jar'):
>             if os.path.isfile(x):

I assume the problem is that you're assuming x is the full path, which it
isn't.  Here's an idiom I've found useful with os.listdir:

  for name in os.listdir(path):
    fullname = os.path.join(path, name)
    ...

After that, you can easily:

    if os.path.isfile(fullname):
       ...

etc.  What's funny is that glob.glob() returns full paths, but os.listdir()
doesn't.  I still occasionally mistakenly just assume that it does.

Cheers,

// m

-






More information about the Python-list mailing list