Hi Kent: Thanks for the suggestions. I appreciate this a lot. I've
been writing Jython for a while now but I'm still no expert. Your
improvements look good to me.
I wrote my version as a class so I could implement a recursive
pattern. My earlier, non-class, attempts wound up recursing by
passing in the new directory path as your rewritten script does. I
found that without using self.variable that the recursive call would
pass the variable by reference and the new iteration would operate on
the original dir parameter.
I'm going to re-read the Jython docs on how variables work in Jython.
On Apr 17, 2006, at 5:22 PM, Kent Johnson wrote:
> Frank Cohen wrote:
>> I often find myself needing an os.walk()-like function when
>> working with file directories in Jython 2.1. I published a short
>> "how to" article describing an approach I use to walk a directory
>> tree in advance of Jython offering os.walk.
>> Please let me know your feedback, comments, criticisms. I'm open
>> to all.
> mmm...a few comments...
> os.path.walk() is not too hard to use and it is available in Jython.
> Your solution seems overly complicated. I don't think you gain
> anything from making it a class, it could be a single function walk
> (directory, process).
> If you do want to make it a class, there is still no reason for
> process, f, fullpath, dw and s to be instance variables, they could
> be local variables of walk().
> You require that process have a method tallyxml(). A better
> interface would be to just pass a callable. If you want to use
> process.tallyxml then pass that as the callable.
> You only process XML files, if you want it to be more general you
> could pass a filter function as well as the processing function.
> if fullpath[-4:].upper()=='.XML' - I think I prefer if
> self.s = str( self.fullpath ) - you don't use this value.
> So I would write it something like this (without a filter function)
> def walk( dir, process ):
> '''walk a directory tree'''
> print "dir =", dir
> for f in os.listdir( dir ):
> fullpath = os.path.join( dir, f)
> if os.path.isdir( fullpath ) and not os.path.islink
> ( fullpath ):
> walk( fullpath, process )
> if os.path.isfile( fullpath ):
> print f
> process( fullpath )
> Hmm, I wonder how hard it would be to write a real version of
> os.walk() in Jython...
> This SF.Net email is sponsored by xPML, a groundbreaking scripting
> that extends applications into web and mobile media. Attend the
> live webcast
> and join the prime developer group breaking into this new coding
> Jython-users mailing list