python import names file and sort alphabetically
Question: I can't figure out why my simple names script will not work. It appears to bug
out on the while loop. I might be calling it wrongly, but I figured I might
try to get an answer here while I continue researching.

    
    
    #!/usr/bin/python
    
    #open the file
    name_file = open('names.txt', 'r')
    
    #read in lines
    names = name_file.readlines()
    
    #close file
    name_file.close()
    
    #loop to place names in array
    index = 0
            while index < len(names):
                 names[index] = names[index].rstrip('\n')
                 index += 1
    #sort
    names.sort()
    
    #print sorted names
    print names
    


Answer: Maybe this works:

    
    
    with open ('names.txt', 'r') as f:
        names = sorted (name.rstrip ('\n') for name in f)
    
    print (names)
    

The `with` takes care of closing the file once you leaf the scope.

