[Tutor] Trouble with os.path.isfile

Carroll, Barry Barry.Carroll at psc.com
Sat Jun 3 01:23:15 CEST 2006


Greetings:

 

One of the functions of my test system web server program is to allow
the user to view the contents of the image file directories and select
files to load onto the hardware.  Obviously, a central part of this
function is to identify and display the names of directories and files
in a specified directory.  I haven't been able to find any canned code
or cookbook entries that do this, so I'm making one from scratch.  Here
is the helper function that gets and returns the  directory and file
names:

 

>>>>>>>>>>>>>>>>>>>>>>>>>> 

def lstdirsnfiles(tdir):

    flst = os.listdir(tdir)

    flst.sort()

    fnames = []

    dnames = ['.', '..']

    for name in flst:

        absfn = os.path.join(tdir, name)

        if os.path.isdir(absfn):

            dnames.append(name)

        elif os.path.isfile(absfn):

             fnames.append(name)

    return dnames, fnames 

>>>>>>>>>>>>>>>>>>>>>>>>>> 

 

The root of the image file directory tree is "/home/iip/images".  When
lstdirsnfiles is run with tdir set appropriately,  the lists returned
contain no additional information: dnames = ['.', '..'] and fnames = [].
I added some tracing statements to see what was happening inside the
function:

 

>>>>>>>>>>>>>>>>>>>>>>>>>> 

def lstdirsnfiles(tdir):

    global DEBUG, dbgfname

    DEBUG = True

    

    flst = os.listdir(tdir)

    flst.sort()

    fnames = []

    dnames = ['.', '..']

    

    if DEBUG:

        dbgf = open(dbgfname,mode="a")

        dbgf.write("\n"+str (time.localtime())+"\n")

        dbgf.write("Entering lstdirsnfiles\n")

        dbgf.write("The directory = %s\n" % tdir)

        dbgf.write("The file list = %s\n" % flst)

 

    for name in flst:

        absfn = os.path.join(tdir, name)

    

        if DEBUG:

            dbgf.write("File path = %s, isfile = %s\n" % 

                           (absfn, os.path.isfile(absfn)))

 

        if os.path.isdir(absfn):

            dnames.append(name)

        elif os.path.isfile(absfn):

             fnames.append(name)

    

    if DEBUG:

        dbgf.write("dnames = %s\n" % dnames)

        dbgf.write("fnames = %s\n" % fnames)

        dbgf.close()

    DEBUG = False

    

    return dnames, fnames

>>>>>>>>>>>>>>>>>>>>>>>>>> 

 

The log vile received the following:

>>>>>>>>>>>>>>>>>>>>>>>>>> 

(2006, 6, 2, 15, 23, 4, 4, 153, 1)

Entering lstdirsnfiles

The directory = /home/iip/images

The file list = ['test1.bmp', 'test2.bmp', 'test3.bmp', 'test4.bmp',
'test5.bmp', 'test6.bmp']

File path = /home/iip/images/test1.bmp, isfile = False

File path = /home/iip/images/test2.bmp, isfile = False

File path = /home/iip/images/test3.bmp, isfile = False

File path = /home/iip/images/test4.bmp, isfile = False

File path = /home/iip/images/test5.bmp, isfile = False

File path = /home/iip/images/test6.bmp, isfile = False

dnames = ['.', '..']

fnames = []

>>>>>>>>>>>>>>>>>>>>>>>>>> 

 

So there are entries in the directory, but the function doesn't
recognize them as files.  

 

Finally, I opened the interpreter and tried doing this, as nearly as
possible, interactively.  I got this:

 

>>>>>>>>>>>>>>>>>>>>>>>>>> 

>>> import os

>>> tdir = '/home/iip/images'

>>> os.listdir(tdir)

['test4.bmp', 'test3.bmp', 'test2.bmp', 'test1.bmp', 'test5.bmp',
'test6.bmp']

>>> flst = os.listdir(tdir)

>>> flst.sort()

>>> flst

['test1.bmp', 'test2.bmp', 'test3.bmp', 'test4.bmp', 'test5.bmp',
'test6.bmp']

>>> for name in flst:

...     absfn = os.path.join(tdir,name)

...     print absfn, "is",

...     if not os.path.isfile(absfn):

...         print "NOT",

...     print "a file"

...

/home/iip/images/test1.bmp is a file

/home/iip/images/test2.bmp is a file

/home/iip/images/test3.bmp is a file

/home/iip/images/test4.bmp is a file

/home/iip/images/test5.bmp is a file

/home/iip/images/test6.bmp is a file

>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 

 

As you can see, the interpreter correctly identifies the strings in the
list as names of files, but my function does not.  I can't see anything
wrong with the logic in the function.  Can any or you see the problem
I'm missing?

 

As always, thanks in advance for your help.

 

Barry

barry.carroll at psc.com

541-302-1107

________________________

We who cut mere stones must always be envisioning cathedrals.

-Quarry worker's creed

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060602/f2ba27b8/attachment-0001.htm 


More information about the Tutor mailing list