> 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

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to