예제 #1
0
파일: 4B.py 프로젝트: etscrivner/dse
def modify_file(file_path):
    """Modifies the values in the given file.

    Arguments:
        file_path(str): A file to be modified
    """
    output_file = prompt_for_output_file(file_path)
    numbers = io.read_numbers_from_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_float("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
예제 #2
0
파일: 4B.py 프로젝트: etscrivner/dse
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
예제 #3
0
파일: 3B.py 프로젝트: etscrivner/dse
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_float("Entry #{}: ".format(num_entries_given + 1))
            value = float(value)
            wfile.write("{}\n".format(value))
            num_entries_given += 1
예제 #4
0
파일: 4B.py 프로젝트: etscrivner/dse
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