Monday, March 1, 2010

March Madness Challenge - MMC01

I'm going to be part of the March Madness Challenge!

I'm will use Python, because I never used it. So don't expect much, just small little helper programs to learn Python.

The first program is a classic: It loops thru a given folder and prints the name of the files that have the extension you want.

#! /usr/bin/python

import os
import sys

def visit(arg, dirname, names):
os.chdir(dirname)
for name in names:
splitName = os.path.splitext(name)
if (splitName[1] == sys.argv[2]):
print name

def usage():
print 'List all files in given folder with specified extension.'
print 'Use MMC01 Path Extension'
print 'Example MMC01.py . .py'

print '***** MMC01 *****'
if (len(sys.argv) != 3):
usage()
else:
os.path.walk(sys.argv[1], visit, 0)


Possible output:

b$ python mmc01.py
***** MMC01 *****
List all files in given folder with specified extension.
Use MMC01 Path Extension
Example MMC01.py . .py

b$ python mmc01.py . .py
***** MMC01 *****
mmc01.py


Sorry about the format messup. I will figure out how to post code with format in here.


No comments:

Post a Comment