« A shorter filesystem recurser in Perl | Main | Giving XMMS the "what for" with Perl (AKA, C developers, append to enums, don't insert) »

A filesystem recurser in Python too

So I'm learning Python here and there and thought I'd reimplement my directory recurser with it. Again, I'm only learning, so there may be a better way to do this in Python. Of course I could use os.walk(), but that would defeat the purpose of creating my own recurser. Why am I doing this simple task? Just cuz. :)

#!/usr/bin/env python

import os, sys

def printDir( path ):
     "Print a directory and it's files recursively"
     
     print 'Directory: ' + path
     
     for item in os.listdir( path ):
          newPath = path + '/' + item
          if os.path.isdir( newPath ):
               printDir( newPath )
          elif os.path.isfile( newPath ):
               print item
          
if len(sys.argv) > 1:
     printDir( sys.argv[1] )
else:
     printDir( '.' )

Next up...Ruby...

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on September 20, 2006 9:51 AM.

The previous post in this blog was A shorter filesystem recurser in Perl.

The next post in this blog is Giving XMMS the "what for" with Perl (AKA, C developers, append to enums, don't insert).

Many more can be found on the main index page or by looking through the archives.