############################################# # Example from Lecture 7 # # Opens silly.txt, counts the number of words # in the file, makes a list consisting of all # occurences of the word "silly", then writes those # occurrences to a new file. look_for = "silly" in_file = "silly.txt" out_file = "really_silly.txt" myfile = open(in_file) count = 0 look_for_list = [] for line_of_text in myfile: print line_of_text[:-1] word_list = line_of_text.split() print word_list for word in word_list: if word == look_for: look_for_list.append(word) print look_for_list count = count + len(word_list) print count myfile.close() outfile = open(out_file, "w") for word in look_for_list: outfile.write(word + "\n") outfile.close()