Пример #1
0
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?')
        # If the file shouldn't be overwritten, abort
        if not should_overwrite:
            return

    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_input(
                'Entry #{}: '.format(num_entries_given + 1))
            value = float(value)
            wfile.write('{}\n'.format(value))
            num_entries_given += 1
Пример #2
0
    def prompt_overwrite_or_change(self, file_path, maximum_list_length):
        """Prompt user to either opt to overwrite the given file or enter a
        new file name.

        Arguments:
            file_path(basestring): The path to the file
            maximum_list_length(int): The maximum allowed list length.
        """
        should_overwrite = io.yes_no_prompt(
            'File {} exists. Overwrite?'.format(file_path))
        if not should_overwrite:
            enter_different_file = io.yes_no_prompt(
                'Would you like to enter a different file name?')
            if enter_different_file:
                self.execute(maximum_list_length)
                sys.exit()
            sys.exit('Aborting. Not overwriting existing file.')
Пример #3
0
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
Пример #4
0
    def prompt_output_file(self, prompt, maximum_attempts=5):
        """Prompt the user to enter a valid output file.

        Arguments:
            prompt(basestring): The prompt to be displayed.
            maximum_attempts(int): Default 5, the maximum number of attempts
                a user has to enter a valid output file name.

        Returns:
            basestring: The output file path.
        """
        while maximum_attempts > 0:
            output_file = io.prompt_valid_file_name(prompt)
            if not os.path.exists(output_file):
                return output_file
            if io.yes_no_prompt(
                    'File {} already exists. Overwrite?'.format(output_file)):
                return output_file
            maximum_attempts -= 1
        raise RuntimeError('Maximum attempts exceeded.')