def directory_does_not_exist(self, file_path, maximum_list_length): """Indicate that the directory does not exist and given user the option to try again. Arguments: file_path(basestring): The path to the file. maximum_list_length(int): The maximum allowed list length. """ print 'error: Directory for file {} does not exist'.format( file_path) io.prompt_try_again_or_abort() self.execute(maximum_list_length) sys.exit()
def write_file(file_path): """Requests a series of numerical inputs and writes them to the file. Arguments: file_path(basestring): The path to the file """ if os.path.isfile(file_path): should_overwrite = io.yes_no_prompt( 'File {} exists. Overwrite?'.format(file_path)) # If the file shouldn't be overwritten, abort if not should_overwrite: enter_different_file = io.yes_no_prompt( 'Would you like to enter a different file name?') if enter_different_file: file_path = io.prompt_valid_file_name('New file name: ') write_file(file_path) sys.exit('Aborting. Not overwriting existing file.') if not os.path.exists(os.path.dirname(file_path)): print 'ERROR: Directory for file {} does not exist'.format(file_path) io.prompt_try_again_or_abort() file_path = io.prompt_valid_file_name('New file name: ') write_file(file_path) total_num_entries = io.get_and_confirm_input( 'How many numbers will you enter: ') total_num_entries = int(total_num_entries) num_entries_given = 0 with open(file_path, 'w') as wfile: while num_entries_given < total_num_entries: value = io.get_and_confirm_float( 'Entry #{}: '.format(num_entries_given + 1)) value = float(value) wfile.write('{}\n'.format(value)) num_entries_given += 1