# Functional Programming & File Reading Example # # CS 1301 - Spring 2009 # Jay Summet # # Both functions will read in a file and print any lines that contain # hash marks ("#"). They will only print from the hash mark to the end of # the line. def printComments(filename): myfile = open(filename,"r") line = myfile.readline() while( len(line) > 0): if ( "#" in line): loc = line.find("#") print line[loc:] line = myfile.readline() myfile.close() def functionalPrintComments(filename): myfile = open(filename,"r") lines = myfile.readlines() myfile.close() #Filter out any lines that don't have '#' marks. comments = filter( lambda s: "#" in s, lines) #Strip out everything before the hasmarks stripped = map(lambda s: s[ s.find("#"): ] , comments ) for s in stripped: print s