28
Sep
09

recursive directory walker using jython



Several months ago I wrote a post related to creating a recursive directory walker in Python. We did not make use of os.walk. While playing with Jython I thought it would be nice to write an equivalent. Here’s what I have :


from java.io import File
import sys

class FileWalker(object):
	def walk(dir,cbf):
	    files = dir.listFiles()
	    for file in files:
	        cbf(file)
	        if file.isDirectory():
	            FileWalker.walk(file,cbf)
	walk = staticmethod(walk)

def list_file(file):
    print "saw %s" % file

if __name__ == "__main__":
    FileWalker.walk(File(sys.argv[1]),list_file)

I think the code is pretty straight-forward. If you’re familiar with Java even a little bit, I’m sure you can understand what’ s going on in here. You should know that the callback function isn’t receiving a string as it’s parameter ( as the first version of the walker did ) . This one receives a Java File object, which means you have access to all the methods present in the File class.

About these ads

0 Responses to “recursive directory walker using jython”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Blog Stats

  • 173,292 hits

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: