[Tutor] tree problem

Roelof Wobben rwobben at hotmail.com
Sun Sep 12 15:02:05 CEST 2010

> Subject: Re: [Tutor] tree problem
> From: evert.rol at gmail.com
> Date: Sun, 12 Sep 2010 13:29:12 +0200
> CC: tutor at python.org
> To: rwobben at hotmail.com
>> Write a program named litter.py that creates an empty file named trash.txt in each subdirectory of a directory tree given the root of the tree as an argument (or the current directory as a default).
>> So I change the example to this :
>> def traverse(path, s='.\n', f=0, d=0):
>> path2file = os.path.join(path) *** pathfile contains the path
>> if os.path.isdir(path2file): **** if pathfile is a dir.
>> d += 1 ***** the is one more directory
>> if getdirlist(path2file): ****** if the outcome of getdirlist is the same as the current directory
>> s, f, d = traverse(path2file, '| ' + s, f, d) do this module again
>> else:
>> f += 1 ****** else f (number of files increases with 1
>> return s, f, d ****** return s , numbers of files and the number of directories.
> That can't be a valid program: no indentation, comments not preceded by a comment mark (#), and getdirlist is nowhere defined afaics. While probably anyone can understand what's the real code of the above snippet, it doesn't help putting non-valid code like this in your email. If you can directly copy-paste code (plain text), that is almost always better.
>> When I try to make it run I get this message :
>> File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 31, in traverse
>> s, f, d = traverse(path2file, '| ' + s, f, d)
>> File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 31, in traverse
>> s, f, d = traverse(path2file, '| ' + s, f, d)
>> File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 31, in traverse
>> s, f, d = traverse(path2file, '| ' + s, f, d)
>> File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 28, in traverse
>> if os.path.isdir(path2file):
>> File "C:\Python27\lib\genericpath.py", line 44, in isdir
>> return stat.S_ISDIR(st.st_mode)
>> File "C:\Python27\lib\stat.py", line 41, in S_ISDIR
>> return S_IFMT(mode) == S_IFDIR
>> RuntimeError: maximum recursion depth exceeded
>> I can't see why this happens.
>> I know I have to fish but I can't see what's wrong here.
>> So I hope someone can learn how to fish here.
> Fishing is debugging. You could use the logging module for that, but assuming you're not familiar with that, litter your program with print statements, and print out the value of the various variables at certain points in your program (it helps putting a short string in the print statement as well, so you know which print statement print where. Eg, print '1:', s, f, d; and then a few lines below, print '2:', s, f, d).
> Having done that (you will get a lot of output), try to follow the logic of the program and see why things happen the way they do. In this case, why you keep spiraling in and never break out of your recursion. Then, step by step, you can try and change or add statements so you actually find a way to break out the recursion.
> Evert
Hello Evert.
Sorry but the change to plain text destroyed also this.
What I have it this.
def getdirlist(path):
    dirlist = os.listdir(path)
    dirlist = [name for name in dirlist if name[0] != '.']
    return dirlist

def traverse(path, s='.\n', f=0, d=0):
    dirlist = getdirlist(path)
    for file in enumerate(dirlist):
        print file
        path2file = os.path.join(path)
        if os.path.isdir(path2file):
            d += 1
            if getdirlist(path2file):
                print path2file
            myfile = open ('trash.txt', 'w')
            myfile.close () 
    return s, f, d
The file is made. Now find out why it doesn't go into the next level.
With this : s, f, d = traverse(path2file, '|   ' + s, f, d) I goes into a loop which never ends.


More information about the Tutor mailing list