Accessing Each Word in a File : read « File « Python Tutorial






filePath = "input.txt"
wordList = []
wordCount = 0

#Read lines into a list
file = open(filePath, 'rU')
for line in file:
    for word in line.split():
        wordList.append(word)
        wordCount += 1
print wordList
print "Total words = %d" % wordCount








12.10.read
12.10.1.Read with index
12.10.2.Read bytes into a buffer
12.10.3.Read entire file into a buffer
12.10.4.Accessing Each Word in a File
12.10.5.exception you are anticipating
12.10.6.function that tries to read a file and return its contents