Adding some config to a juniper device I wrote this script to add some config to a Juniper device.  It works well but the whole user input validation section (yes or no section) seems a little messy.

Is there a better way I could have done it?

from jnpr.junos import Device
from jnpr.junos.utils.config import Config
import getpass

#Grab credentials
username = raw_input("Enter your username:")
password = getpass.getpass("Enter your password:")

#Confirmation of commands
commands = open("commands" , "r")
commands_to_commit = commands.read()
print ""
print "The commands you are about to commit are: "
print commands_to_commit
print ""
print "Do you want to continue to run these commands?"
confirm = raw_input("Y or N: ")
    if confirm in ['n','no','N','NO','No']:
            print "Exiting..."
            quit()
while confirm not in ['y','Y','yes',"Yes",'YES','n','N','NO','No','no']:
    print "Invalid Choice, Try again"
    confirm = raw_input("Y or N: ")
    if confirm in ['n','no','N','NO','No']:
            print "Exiting..."
            quit()
    elif confirm in ['y','Y','yes',"Yes",'YES']:
            continue

#Open a file called swichlist which has a list of devices to modify
with open('switchlist') as infile:
    for host in infile:
            try:
                    print "Working on:", host,
                    #Connect to devices in switchlist
                    dev = Device(host=host.strip(),  user=username, password=password)
                    dev.open()
                    cu = Config(dev)
                    #Looks for a file named commands with the list of commands to run
                    incmd = open('commands')
                    set_cmd = incmd.read()
                    cu.load(set_cmd, format="set")
                    cu.commit()
                    dev.close()
                    print "Completed:", host
            except Exception,e: print "Error:", e
            continue
The confirmation prompt is a self-contained chunk of code with a specific purpose.  It deserves to be extracted into a function:

def confirm(prompt='Y or N: ', yes='YES', no='NO'):
    while True:
        response = raw_input(prompt).upper()
        if yes.startswith(response) and not no.startswith(response):
            return True
        elif no.startswith(response) and not yes.startswith(response):
            return False
        print "Invalid choice. Try again."


(Technically, this is more lenient than your original code, in that it accepts strings like 'yE' as a yes.  I think that's OK.)

Then, in your main program, you can write:

print "Do you want to continue to run these commands?"
if not confirm():
    print "Exiting..."
    quit()




Any call to open() should almost certainly be done using a with block.  You wrote with open('switchlist') as infile, which is good.  However, you also wrote commands = open("commands", "r") and incmd = open('commands'), neither of which you close properly.  But why are you calling incmd = open('commands') at all, when it's just supposed to be commands_to_commit?