# # This program opens a file (the file name is supplied by the user) # with numbers. Extracts them and stores them in a list. # It understands lines of numbers separated by space as well as # number-per-line separation # e.g. # 1 21 32 12 # 9 0 -9 23 1 # OR # 1 # 21 # 32 ... def main(): print("Hello!\ This program opens a file with numbers,\ extracts all the numbers and stores them in a list.\ The file name is supplied by the user.") fname = input("Please, input the name of the file with numbers:") inSource = open(fname,'r') numbers=[] for line in inSource: # iterate over lines in the input file print("Next line from the file: ", line) # display the line taken from the input file # split the string into a list of numbers using " " as a dilimeter, assigns to numbers_in_line nums_in_line = line.split() for item in nums_in_line: # iterate over items in the line numbers.append(int(item)) print("The numbers extracted from the file {0} are:".format(fname)) for item in numbers: print(item, end=", ") inSource.close() main()