Python Programming: An Introduction to Computer Science

(Nora) #1
8.3.COMMONLOOPPATTERNS 125

8.3.3 FileLoops


Onedisadvantageofalltheaveragingprogramspresentedsofaris thatthey areinteractive. Imagineyouare
tryingtoaverage 87 numbersandyouhappento make a typoneartheend.Withourinteractive program,you
willneedtostartalloveragain.
A betterapproachtotheproblemmightbetotypeallofthenumbersintoa file.Thedatainthefilecan
beperusedandeditedbeforesendingit toa programthatgeneratesa report.Thisfile-orientedapproachis
typicallyusedfordataprocessingapplications.
Backin Chapter4, welookedat readingdatafromfilesusingthePythonreadlinesmethodandafor
loop.We canapplythistechniquedirectlytothenumberaveragingproblem.Assumingthatthenumbersare
typedintoa fileoneperline,wecancomputetheaveragewiththisprogram.


average5.py


def main():
fileName = raw_input("Whatfile are the numbers in? ")
infile = open(fileName,’r’)
sum = 0.0
count = 0
for line in infile.readlines():
sum = sum + eval(line)
count = count+ 1
print "\nThe averageof the numbers is", sum / count


Inthiscode,readlinesreadstheentirefileintomemoryasa sequenceofstrings.Theloopvariableline
theniteratesthroughthissequence;eachlineis convertedtoa numberandaddedtotherunningsum.
Onepotentialproblemwiththiskindoffileprocessingloopis thattheentirecontentsofthefilearefirst
readintomainmemoryviathereadlinesmethod. AsyouknowfromChapter1,secondarymemory
wherefilesresideis usuallymuchlargerthantheprimarymemory. It’s possiblethata largedatafilemay
notfitintomemoryallat onetime.Inthatcase,thisapproachtofileprocessingwillbeveryinefficientand,
perhaps,notworkat all.
Withverylargedatafiles,it is bettertoreadandprocesssmallsectionsofthefileat a time.Inthecaseof
textfiles,a simpleapproachis toprocessthefileonelineat a time.Thisis easilyaccomplishedbya sentintel
loopandthePythonreadlinemethodonfiles.Recall,thereadlinemethodgetsthenextlinefroma
fileasa string.Whenwecometotheendofthefile,readlinereturnsanemptystring,whichwecanuse
asa sentinelvalue.Hereis a generalpatternforanend-of-fileloopinPython.


line = infile.readline()
while line != "":


process line


line = infile.readline()


Atfirstglance,youmaybeconcernedthatthisloopstopsprematurelyif it encountersanemptylinein
thefile.Thisis notthecase.Remember, a blanklineina text filecontainsa singlenewlinecharacter("


n"),
andthereadlinemethodincludesthenewlinecharacterinitsreturnvalue.Since"


n" != "", theloop
willcontinue.
Hereis thecodethatresultsfromapplyingtheend-of-filesentinelloopto ournumberaveragingproblem.


average6.py


def main():
fileName = raw_input("Whatfile are the numbers in? ")
infile = open(fileName,’r’)
sum = 0.0
count = 0

Free download pdf