os module - reliable?

curtissol at my-deja.com curtissol at my-deja.com
Sat Dec 30 20:48:09 EST 2000


I am writing exercise 1 pg 212 of "Learning Python". It asks for a
program that prints out the names of all files in a specific directory
and all its subdirectories. The code I have seems relatively straight
forward:

import os, sys

import os, sys

def ShowDirContents(dir):
    contents = os.listdir(dir)

    for file in contents:
        if os.path.isdir(file):
            ShowDirContents(file)
        else:
            print file



if __name__ == "__main__":
    ShowDirContents(os.getcwd()+"\Doc")

However, the test
   if os.path.isdir(file):

seems to only recognize some of subdirectories of "...Python20\Doc".
Those that are recognized it handles just fine. Those it does not
recognize, it simply treats as files. Also, as you may know, the "Doc"
directory contains a subdirectory called 'doc'. When the code sees this,
it goes into an infinite loop only outputing some of files in Doc before
it enters 'doc' again. This behavior goes away if I change the name of
the subdirectory 'doc'.

How reliable are the os and os.path modules? I am running Python 2.0
with NT 4.5 on a PC. Windows NT Explorer shows a number of directories
(folders) that the 'if' test shown above misses.

Thanks


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list