isfile/isdir problem

Jesper Hertel jh at cddk.dk
Fri Mar 3 05:55:32 EST 2000


Hi Jason,

os.path.isdir and .isfile needs the full file name of the file.

Try this:

import os;

sDirectory = "d:\data";

def process_directory(sArg, sDirName, lNames):
    print "\nProcessing directory %s (%d items)..." %
(sDirName,len(lNames));
    for sItem in lNames:
        fullFileName=os.path.join(sDirName, sItem)
        if (os.path.isdir(fullFileName)):
            print "    - Directory: %s" % sItem;
        elif (os.path.isfile(fullFileName)):
            print "    - File: %s." % sItem;
        else:
            print "    - Unrecognized: %s" % sItem;


os.path.walk(os.path.normpath(sDirectory), process_directory, "")


By the way, I recommend you use sDirectory = r"d:\data", that is, use a raw
string instead of a normal one, otherwise you get into trouble when you
change the file name to e.g. "d:\temp", as "\t" is a tab character.

Another thing is that you do not need to put ";" after each statement,
unless you want to.


Jesper Hertel

Alexander, Jason <jalexander at aivia.net> wrote in message
news:D1E34549DAC3D311A05D0020940F00FF06196E at exchmail.velocityenterprises.net
...
> Hello all,
>
> With the help of you guys, I've got the directory traversal down pat
(Thanks
> again, by the way!). Now, I'm having issues with isfile and isdir
returning
> incorrectly. Here's the code:
>
>
> import os;
>
> sDirectory = "d:\data";
>
> def process_directory(sArg, sDirName, lNames):
> print "\nProcessing directory %s (%d items)..." %
> (sDirName,len(lNames));
> for sItem in lNames:
> if (os.path.isdir(sItem)):
> print "    - Directory: %s" % sItem;
> elif (os.path.isfile(sItem)):
> print "    - File: %s." % sItem;
> else:
> print "    - Unrecognized: %s" % sItem;
>
>
> os.path.walk(os.path.normpath(sDirectory), process_directory, "")
>
>
>
> Basically, I'm finding that everything is returning as "Unrecognized",
> except for my current directory, and any files within it. By the way, this
> is on Win32.
>
> Any ideas on what could be causing this? Is this a bug, or am I
overlooking
> something?
>
>
> TIA,
> -Jason
>





More information about the Python-list mailing list