[Tutor] tree problem

2010-09-12 Thread Roelof Wobben


Hello, 

I have this problem.

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.


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.

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


Re: [Tutor] tree problem

2010-09-12 Thread Evert Rol
 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


Re: [Tutor] tree problem

2010-09-12 Thread Lie Ryan
On 09/12/10 21:15, Roelof Wobben wrote:
 
 
 Hello, 
 
 I have this problem.
 
 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). 

By default, Python has a recursion limit of 1000 deep; that is, your
function is calling itself 1000 times without returning.

In this case, the only reason why you hit the recursion limit is if you
have a directory which is 1000 deep (quite unlikely, Windows has a
directory depth limit much lower than that).

Or your function somehow never returns, in a typical recursive function,
it's usually because you have problem in the precondition.

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


Re: [Tutor] tree problem

2010-09-12 Thread Roelof Wobben




 Subject: Re: [Tutor] tree problem
 From: evert@gmail.com
 Date: Sun, 12 Sep 2010 13:29:12 +0200
 CC: tutor@python.org
 To: rwob...@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] != '.']
dirlist.sort()
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.
 
Roelof


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


Re: [Tutor] tree problem

2010-09-12 Thread Joel Goldstick
On Sun, Sep 12, 2010 at 7:54 AM, Lie Ryan lie.1...@gmail.com wrote:

 On 09/12/10 21:15, Roelof Wobben wrote:
 
 
  Hello,
 
  I have this problem.
 
  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).

 By default, Python has a recursion limit of 1000 deep; that is, your
 function is calling itself 1000 times without returning.

 In this case, the only reason why you hit the recursion limit is if you
 have a directory which is 1000 deep (quite unlikely, Windows has a
 directory depth limit much lower than that).

 Or your function somehow never returns, in a typical recursive function,
 it's usually because you have problem in the precondition.

 You really have two problems here:

1. You need to know how to write an empty file with the name litter.py.  You
should probably write a function to see if you can do that.  That's pretty
easy

2. You need to traverse a tree.  I see you are using os module.  You should
try help(os) while in your python shell to learn what methods are
available.  Traversing a tree is also sometimes called 'walking'

good luck


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


Re: [Tutor] tree problem

2010-09-12 Thread Joel Goldstick
On Sun, Sep 12, 2010 at 9:32 AM, Roelof Wobben rwob...@hotmail.com wrote:



 
  Date: Sun, 12 Sep 2010 09:08:18 -0400
  From: joel.goldst...@gmail.com
  To: tutor@python.org

  Subject: Re: [Tutor] tree problem
 
 
 
  On Sun, Sep 12, 2010 at 7:54 AM, Lie Ryan
   wrote:
  On 09/12/10 21:15, Roelof Wobben wrote:
  
  
   Hello,
  
   I have this problem.
  
   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).
 
  By default, Python has a recursion limit of 1000 deep; that is, your
  function is calling itself 1000 times without returning.
 
  In this case, the only reason why you hit the recursion limit is if you
  have a directory which is 1000 deep (quite unlikely, Windows has a
  directory depth limit much lower than that).
 
  Or your function somehow never returns, in a typical recursive function,
  it's usually because you have problem in the precondition.
 
  You really have two problems here:
 
  1. You need to know how to write an empty file with the name
  litter.py. You should probably write a function to see if you can do
  that. That's pretty easy
 
  2. You need to traverse a tree. I see you are using os module. You
  should try help(os) while in your python shell to learn what methods
  are available. Traversing a tree is also sometimes called 'walking'
 
  good luck
 
 
  --
  Joel Goldstick
 
 
  ___ Tutor maillist -

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

 Hello Joel.

 Youre right.
  Problem 1 is easily solved by using myfile = open ('filename', 'w')
 followed by myfile.close()

 Problem 2 is more difficult.

 I have to use recursion and as example the source of the tree command in
 linux is given.
 The traverse module looks like this :

  def traverse(path, prefix='|--', s='.\n', f=0, d=0):


what's up with the prefix???


 dirlist = getdirlist(path)
 for num, file in enumerate(dirlist):


why are you using enumerate?  it gives you num which you never use
 for file in dirlist gives what you seem to be using

 lastprefix = prefix[:-3] + '``--'
 dirsize = len(dirlist)
 if num  dirsize - 1:
 s += '%s %s\n' % (prefix, file)
 else:
 s += '%s %s\n' % (lastprefix, file)
 path2file = os.path.join(path, file)

 if os.path.isdir(path2file):
 d += 1
 if getdirlist(path2file):
 s, f, d = traverse(path2file, '|   ' + prefix, s, f, d)
 else:
 f += 1
 return s, f, d

 For me it looks like the pathfile = os.path.join(path, file)
 and then the s.f.d. rule take care that a subdir is entered.

what are s.f.d.  Can you use more descriptive names


 Am I right on this ?

 Roelof






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


Re: [Tutor] tree problem

2010-09-12 Thread Roelof Wobben




 Date: Sun, 12 Sep 2010 09:46:08 -0400
 From: joel.goldst...@gmail.com
 To: tutor@python.org
 Subject: Re: [Tutor] tree problem



 On Sun, Sep 12, 2010 at 9:32 AM, Roelof Wobben
 wrote:


 
 Date: Sun, 12 Sep 2010 09:08:18 -0400
 From: joel.goldst...@gmail.com
 To: tutor@python.org

 Subject: Re: [Tutor] tree problem



 On Sun, Sep 12, 2010 at 7:54 AM, Lie Ryan
 wrote:
 On 09/12/10 21:15, Roelof Wobben wrote:


 Hello,

 I have this problem.

 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).

 By default, Python has a recursion limit of 1000 deep; that is, your
 function is calling itself 1000 times without returning.

 In this case, the only reason why you hit the recursion limit is if you
 have a directory which is 1000 deep (quite unlikely, Windows has a
 directory depth limit much lower than that).

 Or your function somehow never returns, in a typical recursive function,
 it's usually because you have problem in the precondition.

 You really have two problems here:

 1. You need to know how to write an empty file with the name
 litter.py. You should probably write a function to see if you can do
 that. That's pretty easy

 2. You need to traverse a tree. I see you are using os module. You
 should try help(os) while in your python shell to learn what methods
 are available. Traversing a tree is also sometimes called 'walking'

 good luck


 --
 Joel Goldstick


 ___ Tutor maillist -

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

 Hello Joel.

 Youre right.
 Problem 1 is easily solved by using myfile = open ('filename', 'w')
 followed by myfile.close()

 Problem 2 is more difficult.

 I have to use recursion and as example the source of the tree command
 in linux is given.
 The traverse module looks like this :

 def traverse(path, prefix='|--', s='.\n', f=0, d=0):

 what's up with the prefix???

 dirlist = getdirlist(path)
 for num, file in enumerate(dirlist):

 why are you using enumerate? it gives you num which you never use
 for file in dirlist gives what you seem to be using
 lastprefix = prefix[:-3] + '``--'
 dirsize = len(dirlist)
 if num  dirsize - 1:
 s += '%s %s\n' % (prefix, file)
 else:
 s += '%s %s\n' % (lastprefix, file)
 path2file = os.path.join(path, file)

 if os.path.isdir(path2file):
 d += 1
 if getdirlist(path2file):
 s, f, d = traverse(path2file, '| ' + prefix, s, f, d)
 else:
 f += 1
 return s, f, d

 For me it looks like the pathfile = os.path.join(path, file)
 and then the s.f.d. rule take care that a subdir is entered.
 what are s.f.d. Can you use more descriptive names

 Am I right on this ?

 Roelof





 --
 Joel Goldstick


 ___ Tutor maillist -
 Tutor@python.org To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor
 
Hello, 
 
I solved this by this programm :
 
import os
import sys

def getroot():
if len(sys.argv) == 1:
path = ''
else:
path = sys.argv[1]
if os.path.isabs(path):
tree_root = path
else:
tree_root = os.path.join(os.getcwd(), path)
return tree_root

def getdirlist(path):
dirlist = os.listdir(path)
dirlist = [name for name in dirlist if name[0] != '.']
dirlist.sort()
return dirlist

def traverse(path, s='.\n', f=0, d=0):
file = os.path.join(path,'trash.txt')
myfile = open (file, 'w')
myfile.close()
dirlist = getdirlist(path)
for num, file in enumerate(dirlist):
dirsize = len(dirlist)
if num  dirsize - 1:
s += '%s \n' % (file)
else:
s += '%s \n' % (file)
path2file = os.path.join(path, file)
if os.path.isdir(path2file):
d += 1
s, f, d = traverse(path2file, '|   ' + s, f, d)
return s,f,d

if __name__ == '__main__':
root =  getroot()
tree_str, files, dirs = traverse(root)

 
Roelof

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


Re: [Tutor] tree problem

2010-09-12 Thread Joel Goldstick
On Sun, Sep 12, 2010 at 10:48 AM, Roelof Wobben rwob...@hotmail.com wrote:




 
  Date: Sun, 12 Sep 2010 09:46:08 -0400
  From: joel.goldst...@gmail.com
  To: tutor@python.org
  Subject: Re: [Tutor] tree problem
 
 
 
  On Sun, Sep 12, 2010 at 9:32 AM, Roelof Wobben
  wrote:
 
 
  
  Date: Sun, 12 Sep 2010 09:08:18 -0400
  From: joel.goldst...@gmail.com
  To: tutor@python.org
 
  Subject: Re: [Tutor] tree problem
 
 
 
  On Sun, Sep 12, 2010 at 7:54 AM, Lie Ryan
  wrote:
  On 09/12/10 21:15, Roelof Wobben wrote:
 
 
  Hello,
 
  I have this problem.
 
  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).
 
  By default, Python has a recursion limit of 1000 deep; that is, your
  function is calling itself 1000 times without returning.
 
  In this case, the only reason why you hit the recursion limit is if you
  have a directory which is 1000 deep (quite unlikely, Windows has a
  directory depth limit much lower than that).
 
  Or your function somehow never returns, in a typical recursive function,
  it's usually because you have problem in the precondition.
 
  You really have two problems here:
 
  1. You need to know how to write an empty file with the name
  litter.py. You should probably write a function to see if you can do
  that. That's pretty easy
 
  2. You need to traverse a tree. I see you are using os module. You
  should try help(os) while in your python shell to learn what methods
  are available. Traversing a tree is also sometimes called 'walking'
 
  good luck
 
 
  --
  Joel Goldstick
 
 
  ___ Tutor maillist -
 
  Tutor@python.org To unsubscribe or change
  subscription options:
  http://mail.python.org/mailman/listinfo/tutor
 
  Hello Joel.
 
  Youre right.
  Problem 1 is easily solved by using myfile = open ('filename', 'w')
  followed by myfile.close()
 
  Problem 2 is more difficult.
 
  I have to use recursion and as example the source of the tree command
  in linux is given.
  The traverse module looks like this :
 
  def traverse(path, prefix='|--', s='.\n', f=0, d=0):
 
  what's up with the prefix???
 
  dirlist = getdirlist(path)
  for num, file in enumerate(dirlist):
 
  why are you using enumerate? it gives you num which you never use
  for file in dirlist gives what you seem to be using
  lastprefix = prefix[:-3] + '``--'
  dirsize = len(dirlist)
  if num  dirsize - 1:
  s += '%s %s\n' % (prefix, file)
  else:
  s += '%s %s\n' % (lastprefix, file)
  path2file = os.path.join(path, file)
 
  if os.path.isdir(path2file):
  d += 1
  if getdirlist(path2file):
  s, f, d = traverse(path2file, '| ' + prefix, s, f, d)
  else:
  f += 1
  return s, f, d
 
  For me it looks like the pathfile = os.path.join(path, file)
  and then the s.f.d. rule take care that a subdir is entered.
  what are s.f.d. Can you use more descriptive names
 
  Am I right on this ?
 
  Roelof
 
 
 
 
 
  --
  Joel Goldstick
 
 
  ___ Tutor maillist -
  Tutor@python.org To unsubscribe or change subscription options:
  http://mail.python.org/mailman/listinfo/tutor

 Hello,

 I solved this by this programm :

 import os
 import sys

 def getroot():
if len(sys.argv) == 1:
path = ''
else:
path = sys.argv[1]
if os.path.isabs(path):
tree_root = path
else:
tree_root = os.path.join(os.getcwd(), path)
return tree_root

 def getdirlist(path):
dirlist = os.listdir(path)
 dirlist = [name for name in dirlist if name[0] != '.']
dirlist.sort()
return dirlist

 def traverse(path, s='.\n', f=0, d=0):
file = os.path.join(path,'trash.txt')
myfile = open (file, 'w')
myfile.close()
 dirlist = getdirlist(path)
for num, file in enumerate(dirlist):
 dirsize = len(dirlist)
if num  dirsize - 1:
 s += '%s \n' % (file)
else:
 s += '%s \n' % (file)
 path2file = os.path.join(path, file)
if os.path.isdir(path2file):
d += 1
 s, f, d = traverse(path2file, '|   ' + s, f, d)
return s,f,d

 if __name__ == '__main__':
root =  getroot()
tree_str, files, dirs = traverse(root)


 Roelof


Good for you.  Some questions:

   What do you mean to be happening here:

   if num  dirsize - 1:
s += '%s \n' % (file)
   else:
s += '%s \n' % (file)

it does the same thing in either case

What exactly does s do for you?

You use the first parameter 'path' in your calls to traverse, but  I don't
see how you are using f or d anywhere either
-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tree problem

2010-09-12 Thread Roelof Wobben




 Date: Sun, 12 Sep 2010 13:40:09 -0400
 Subject: Re: [Tutor] tree problem
 From: joel.goldst...@gmail.com
 To: rwob...@hotmail.com



 On Sun, Sep 12, 2010 at 1:20 PM, Roelof Wobben
 wrote:



 
 Date: Sun, 12 Sep 2010 11:59:07 -0400
 From: joel.goldst...@gmail.com
 To: tutor@python.org
 Subject: Re: [Tutor] tree problem



 On Sun, Sep 12, 2010 at 10:48 AM, Roelof Wobben
 wrote:



 
 Date: Sun, 12 Sep 2010 09:46:08 -0400
 From: joel.goldst...@gmail.com
 To: tutor@python.org
 Subject: Re: [Tutor] tree problem



 On Sun, Sep 12, 2010 at 9:32 AM, Roelof Wobben
 wrote:


 
 Date: Sun, 12 Sep 2010 09:08:18 -0400
 From: joel.goldst...@gmail.com
 To: tutor@python.org

 Subject: Re: [Tutor] tree problem



 On Sun, Sep 12, 2010 at 7:54 AM, Lie Ryan
 wrote:
 On 09/12/10 21:15, Roelof Wobben wrote:


 Hello,

 I have this problem.

 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).

 By default, Python has a recursion limit of 1000 deep; that is, your
 function is calling itself 1000 times without returning.

 In this case, the only reason why you hit the recursion limit is if you
 have a directory which is 1000 deep (quite unlikely, Windows has a
 directory depth limit much lower than that).

 Or your function somehow never returns, in a typical recursive function,
 it's usually because you have problem in the precondition.

 You really have two problems here:

 1. You need to know how to write an empty file with the name
 litter.py. You should probably write a function to see if you can do
 that. That's pretty easy

 2. You need to traverse a tree. I see you are using os module. You
 should try help(os) while in your python shell to learn what methods
 are available. Traversing a tree is also sometimes called 'walking'

 good luck


 --
 Joel Goldstick


 ___ Tutor maillist -

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

 Hello Joel.

 Youre right.
 Problem 1 is easily solved by using myfile = open ('filename', 'w')
 followed by myfile.close()

 Problem 2 is more difficult.

 I have to use recursion and as example the source of the tree command
 in linux is given.
 The traverse module looks like this :

 def traverse(path, prefix='|--', s='.\n', f=0, d=0):

 what's up with the prefix???

 dirlist = getdirlist(path)
 for num, file in enumerate(dirlist):

 why are you using enumerate? it gives you num which you never use
 for file in dirlist gives what you seem to be using
 lastprefix = prefix[:-3] + '``--'
 dirsize = len(dirlist)
 if num  dirsize - 1:
 s += '%s %s\n' % (prefix, file)
 else:
 s += '%s %s\n' % (lastprefix, file)
 path2file = os.path.join(path, file)

 if os.path.isdir(path2file):
 d += 1
 if getdirlist(path2file):
 s, f, d = traverse(path2file, '| ' + prefix, s, f, d)
 else:
 f += 1
 return s, f, d

 For me it looks like the pathfile = os.path.join(path, file)
 and then the s.f.d. rule take care that a subdir is entered.
 what are s.f.d. Can you use more descriptive names

 Am I right on this ?

 Roelof





 --
 Joel Goldstick


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

 Hello,

 I solved this by this programm :

 import os
 import sys

 def getroot():
 if len(sys.argv) == 1:
 path = ''
 else:
 path = sys.argv[1]
 if os.path.isabs(path):
 tree_root = path
 else:
 tree_root = os.path.join(os.getcwd(), path)
 return tree_root

 def getdirlist(path):
 dirlist = os.listdir(path)
 dirlist = [name for name in dirlist if name[0] != '.']
 dirlist.sort()
 return dirlist

 def traverse(path, s='.\n', f=0, d=0):
 file = os.path.join(path,'trash.txt')
 myfile = open (file, 'w')
 myfile.close()
 dirlist = getdirlist(path)
 for num, file in enumerate(dirlist):
 dirsize = len(dirlist)
 if num  dirsize - 1:
 s += '%s \n' % (file)
 else:
 s += '%s \n' % (file)
 path2file = os.path.join(path, file)
 if os.path.isdir(path2file):
 d += 1
 s, f, d = traverse(path2file, '| ' + s, f, d)
 return s,f,d

 if __name__ == '__main__':
 root = getroot()
 tree_str, files, dirs = traverse(root)


 Roelof


 Good for you. Some questions:

 What do you mean to be happening here:

 if num  dirsize - 1:
 s += '%s \n' % (file)
 else:
 s += '%s \n' % (file)

 it does the same thing in either case

 What exactly does s do for you?

 You use the first parameter 'path' in your calls to traverse, but I
 don't see how you are using f or d anywhere either
 --
 Joel Goldstick


 ___ Tutor maillist -
 Tutor@python.org To unsubscribe or change
 subscription options:
 http

Re: [Tutor] tree problem

2010-09-12 Thread Alan Gauld


Lie Ryan lie.1...@gmail.com wrote

In this case, the only reason why you hit the recursion limit is if 
you

have a directory which is 1000 deep (quite unlikely, Windows has a
directory depth limit much lower than that).


Or you accidentally have a recursive directory - one that contains a
shortcut to a folder higher up in the chain. These are easy to create
in Windows but nearly impossible tyo remove.

I have one such folder in my work PC(*) that I renamed to
BAD FOLDER DO NOT USE and have to remove from every
backup I ever make because it causees infinite looping.

(*) I will point out that I didn't create it - it was a malfunctioning
application that I was using in Beta... And if anyone knows how
to remove it I'd be very grateful!

Alan G.


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