def modify_file(file_path): """Modifies the values in the given file. Arguments: file_path(str): A file to be modified """ numbers = io.read_numbers_from_file(file_path) output_file = prompt_for_output_file(file_path) # Go through each number one at a time results = [] for idx, each in enumerate(numbers): # Display the results up to this point if results: print 'Numbers So Far:' for num in results: print num # Otherwise display next number print 'Next Number:' print each # Let the user choose what to do with the number choice = io.choose_from_list( 'What would you like to do', ['Keep', 'Modify', 'Delete', 'Keep Rest'] ) choice = choice.lower() if choice == 'keep': results.append(each) elif choice == 'modify': number = io.get_and_confirm_input("New value: ") results.append(float(number)) elif choice == 'keep rest': results += numbers[idx:] break elif choice == 'delete': # Do nothing pass # Write the updated numbers to the output file io.write_numbers_to_file(output_file, results) print 'Results written to ', output_file
def add_file(file_path): """Go through file line-by-line and ask for additions after given line. Arguments: file_path(str): The path to the file """ numbers = io.read_numbers_from_file(file_path) output_file = prompt_for_output_file(file_path) # Go through each number one at a time results = [] for idx, each in enumerate(numbers): # Display the results up to this point if results: print 'Numbers So Far:' for num in results: print num print 'Next number:' print each # Get the choice choice = io.choose_from_list( 'What would you like to do:', ['Keep', 'Add Number After', 'Keep Rest']) choice = choice.lower() if choice == 'keep': results.append(each) elif choice == 'add number after': # Get the new number and add it to the list results.append(each) number = io.get_and_confirm_float("New number: ") results.append(float(number)) elif choice == 'keep rest': # Add remaining numbers to results and exit loop results += numbers[idx:] break # Write the updated numbers to the output file io.write_numbers_to_file(output_file, results) print 'Results written to', output_file