# randomize the lines in a file # input file is list.txt, output file is listu.txt # # Matt Bishop, MHI 289I, Winter 2019 # import random def main(): # # open the file and read in the lines # try: # file to read inf = open("fileio1.lst", "r") flines = inf.readlines() num = len(flines) # file to write outf = open("fileio1.rlst", "w") # handle file errors 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: outf.writelines(flines) except Exception as errmsg: print(errmsg) return # # clean up and quit # inf.close() outf.close() main()