Thursday, March 31, 2011

Well, Really, I Am

Talk about a tough semester!  I have several group projects to finish up, as well as a research paper.  I would like to post some homework, but many of my written assignments are critiques of 1) data management plans from federal agencies, or 2) digital libraries.  I am not sure how wise it would be, for a student to post such critiques.  Is it strange, that I care so much about offending people I will (probably) never meet?  Perhaps I should just blot the names out and have people figure it out themselves.

In any case, I do not think I'll be posting my standard homework pieces from this semester.  I will, however, provide some Python programming:

#---------------------------------------------------------------------------------
#5.6:  Write a program that reads in a file and then prints out the number of lines, words, and characters in a file.
#---------------------------------------------------------------------------------
def fileCounts(fileName):
        fileObj = open(fileName)

#makes a list of all of the lines in a file
        lineList = fileObj.readlines() 

#gets amount of items in lineList 
        lineCount = len(lineList) 

#setting counters      
        wordCount = 0                   
        charCount = 0
        for theLine in lineList:

#increase wordCount by the result of len(theline.split())
                wordCount = wordCount + len(theLine.split()) 

#increase charCount by the result of len(theLine)  
                charCount = charCount + len(theLine)           
        print ("There are", lineCount, "lines", wordCount, "words and", charCount, "characters in this document")


Who would have known that I would get into Python programming?  This is one of my later homework assignments- the first few chapters were hard, but only for me.  The rest of the Python programming world would laugh at the hours I spent staring at my computer screen, not knowing what to do.  At least I know that I am learning something.

No comments:

Post a Comment