# randomize the lines in a file # input file is list.txt, output file is listu.txt # Matt Bishop, ECS 10, Fall 2012 import random def main(): # # open the file and read in the lines # try: # file to read f = open("list.txt", "r") flines = f.readlines() num = len(flines) # file to write g = open("listu.txt", "w") except Exception as errmsg: print(errmsg) return # # randomize the order of the lines # doing it num times is probably overkill! # for i in range(num): line1 = random.randrange(num) line2 = random.randrange(num) flines[line1], flines[line2] = flines[line2], flines[line1] # # now write out the lines # try: g.writelines(flines) except Exception as errmsg: print(errmsg) return # # clean up and quit # f.close() g.close() main()