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