1 min read

Hello Python

Inspired by Jian’s polyglot programming practice, I also begin to brush up Python and C++ which I learned during graduate school. Following is a Python response to one of Jian Dai’s former programming challenges for lines count of source codes:

[cce lang=”python”]

import os

#count number of lines of

#single file

def lineCount(fileName):

countSingle=0

for line in open(fileName):

countSingle += 1

return countSingle

#count number of lines of

#directory and subdirectories

def dirCount(dir,extension):

countTotal=0

for r,d,f in os.walk(dir):

for files in f:

if files.endswith(extension):

fileName=os.path.join(r,files)

countSingle=lineCount(fileName)

countTotal += countSingle

return countTotal

a=dirCount(“C:/Program Files/CDISC Express/”,”.sas”)

print a

[/cc]

I use python-2.7.2, the final Python 2.x release most because of the various modules support for learning purpose. The book helps me to get the quick review of Python is _Think Python: How to Think Like a Computer Scientist_ by Allen Downey.

Also, I begin to use CodeColorer for this blog to insert codes.