Not sure if you want to read data from file or write to it. I may be wrong but Going by the "logic structure" in code I guess is you want to collect names of students & dump them to a file.
But the code writeen does the opposite & reads from file.
Anyway I have got the below code working , provided there is a file "student_names.txt" present in current dir & has some data available.
Based on this you can alter it to write names .
student_name = "charles", "Chelsea", "Mary"
name3_end = "Mary"
num_students = 0
string = student_name
num_students = int(input('how many students in class:'))
print('Number of students in class are', num_students)
#open the names.txt file for reading
name_file = open('student_names.txt', 'r')
for count in range(1, num_students + 1):
student_name = str(input('enter the name of student:'))
#read the names to file
fileData = str (name_file.read())
print('Data read from names.txt.', fileData )
#close the File
print('Data read to names.txt.')
print('The student that would be front of the line is', name1_front)
print('The student that would be end of the line is', name3_end)
#call the main function