[Tutor] Listing files in directories recursively

Sheila King sheila@thinkspot.net
Sun, 15 Apr 2001 22:59:54 -0700


OK, I solved my problem already. Here is what I changed:

def listFiles(dir):
    basedir = dir
    print "Files in ", os.path.abspath(dir), ": "
    subdirlist = []
    for item in os.listdir(dir):
        if os.path.isfile(item):
            print item
        else:
            subdirlist.append(os.path.join(basedir, item))
    for subdir in subdirlist:
        listFiles(subdir)


Changed the line above:
    print "Files in ", os.path.abspath(dir), ": "

To
    print "Files in ", dir, ": "

I don't figure that had anything to do with the error, but putting abspath on
it wouldn't really help. Plus, I've found that abspath just really tacks on
the path for the current working directory, which isn't really what I want,
here.

The key change, was changing this line:
        if os.path.isfile(item):

To this:
        if os.path.isfile(os.path.join(basedir,item)):


The files systems are kind of tricky to work with, because the files must be
clearly indicated, by full path.

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/


On Sun, 15 Apr 2001 22:50:23 -0700, Sheila King <sheila@thinkspot.net>  wrote
about [Tutor] Listing files in directories recursively:

:OK, so I'm practicing a bit using os and os.path modules. I think I will try
:to list all the files under a given root directory, recursively going through
:all of its subdirectories and listing their contents, too. Shouldn't be too
:hard?
:
:OK, here's my script in its current version:
:
:-----------------------------------------------------
:import sys, os
:
:'''print all the files in a directory and recursively
:in all directories below the given starting directory'''
:
:def listFiles(dir):
:    basedir = dir
:    print "Files in ", os.path.abspath(dir), ": "
:    subdirlist = []
:    for item in os.listdir(dir):
:        if os.path.isfile(item):
:            print item
:        else:
:            subdirlist.append(os.path.join(basedir, item))
:    for subdir in subdirlist:
:        listFiles(subdir)
:
:startdir = sys.argv[1]  #starting dir passed as argument to script
:print "startdir is ", startdir
:listFiles(startdir)
:-----------------------------------------------------
:
:Here is the command line call to the script:
:
:-----------------------------------------------------
:E:>python E:\programs\LearningPython\recursfilelist.py C:\temp\
:-----------------------------------------------------
:
:
:Here is the output:
:-----------------------------------------------------
:startdir is  C:\temp\
:Files in  C:\temp : 
:Files in  C:\temp\Courses : 
:Files in  C:\temp\Courses\APCS : 
:Files in  C:\temp\Courses\APCS\KARELWIN : 
:Files in  C:\temp\Courses\APCS\KARELWIN\BC450RTL.DLL : 
:Traceback (most recent call last):
:  File "E:\programs\LearningPython\recursfilelist.py", line 20, in ?
:    listFiles(startdir)
:  File "E:\programs\LearningPython\recursfilelist.py", line 16, in listFiles
:    listFiles(subdir)
:  File "E:\programs\LearningPython\recursfilelist.py", line 16, in listFiles
:    listFiles(subdir)
:  File "E:\programs\LearningPython\recursfilelist.py", line 16, in listFiles
:    listFiles(subdir)
:  File "E:\programs\LearningPython\recursfilelist.py", line 16, in listFiles
:    listFiles(subdir)
:  File "E:\programs\LearningPython\recursfilelist.py", line 10, in listFiles
:    for item in os.listdir(dir):
:WindowsError: [Errno 3] The system cannot find the path specified:
:'C:\\temp\\Courses\\APCS\\KARELWIN\\BC450RTL.DLL'
:-----------------------------------------------------
:
:Now, I don't know why it isn't listing any of the files. The listFiles
:function clearly tests if item is a file, using os.path.isfile, and if so,
:should list the file before calling the recursion to the next directory level
:below. But it isn't listing any of the files. Moreover, it's considering a
:.dll file to be a directory?
:
:I've been playing with this for two days, now. (Actually, my original idea was
:to add up the sizes of all the files, but I've put the file sizes on hold,
:until I can just list the blasted files.)
:
:I must be doing something simple wrong. Does anyone see what it is?