>>> # Reading/Writing text files >>> >>> # writing out a text file >>> outputFile = open('output.txt', 'w') >>> numberRange = range(100) >>> for number in numberRange: ... outputFile.write(str(number) + "\t" + str(number*2) + "\n") ... >>> outputFile.close() >>> >>> # reading in the text file we just wrote >>> inputFile = open('output.txt', 'r') >>> for line in inputFile: ... tokens = line.split() ... print tokens[1] + " " + str(len(tokens)) ... >>> inputFile.close() >>> >>> # a slightly more verbose way to read the same file >>> inputFile = open('output.txt', 'r') >>> line = inputFile.readline() >>> while len(line) > 0: ... tokens = line.split() ... print tokens[1] + " " + str(len(tokens)) ... line = inputFile.readline() ... >>> inputFile.close() >>> |