#------------------------------------------------------------------------------ # RandomInput6.py # Creates a random input file for CSE 101 pa6 by selecting a number of lines # at random from a file. The number of lines to select is given by user input. # Note that the same line may be selected multiple times. # # To run this program, do # # python3 RandomInput6.py # #------------------------------------------------------------------------------ from random import choice # open file to randomize file1 = open( input('Enter name of file to read from: '),'r') # get file to write to file2 = open( input('Enter name of file to write to: '),'w') # get number of lines to write to file n = int( input('Enter the number of lines to write: ') ) # select n lines from file1 (with replacement) R = file1.readlines() W = [] for i in range(n): W.append(choice(R)) # write random lines to file2 for line in W: file2.write(line) # close files file1.close() file2.close()