# -*- coding: utf-8 -*- # Loop version #def linesFromFile(filename): # '''Returns a list of all the lines from a # file with the given filename. In each line, # the terminating newline has been removed.''' # # strippedLines = [] # inFile = open(filename, 'r') # lines = inFile.readlines() # for line in lines: # strippedLines.append(line.strip()) # remove trailing newline # inFile.close() # return strippedLines # List comprehension version #def linesFromFile(filename): # '''Returns a list of all the lines from a # file with the given filename. In each line, # the terminating newline has been removed.''' # # inFile = open(filename, 'r') # strippedLines = [line.strip() # for line in inFile.readlines()] # inFile.close() # return strippedLines def linesFromFile(filename): '''Returns a list of all the lines from a file with the given filename. In each line, the terminating newline has been removed.''' with open(filename, 'r') as inputFile: return [line.strip() for line in inputFile.readlines()] try: memories = linesFromFile('memories.txt') except IOError: memories = [] def printMemories(): '''Print a numbered list of all memories''' for i in range(0, len(memories)): print(str(i+1) + '. ' + memories[i]) def remember(mem): '''Remember mem persistently and print all memories''' memories.append(mem) # Add mem to end of list with open('memories.txt', 'a') as memFile: memFile.write(mem + '\n') # Add mem to end of file printMemories() # Print all memories