Ex No: 19 File Handling Operations
Aim: To write a python program to perfrom File Handling Operations.
Algorithm
Step1: Get Student names in a list and enter 'quit' to exit.
Step2: Sort the list using sort() function.
Step3: Open 'studlist.txt' file in write ('w') mode.
Step4: Write the names from the list into file with roll number and close the file.
Step5: Open 'studlist.txt' file in read ('r') mode.
Step6: Using for loop display each line in the file one by one.
Source Code
stud_names=list()
print "Enter Names one by one ('quit' to exit)"
val=""
while 1:
    val = raw_input("Student Name: ")
    if val == 'quit':
        break
    stud_names.append(val)

#sorting names
stud_names.sort()
#print stud_names

i=1
fo = open("studlist.txt","w")
for name in stud_names:
    fo.write("%d\t%s\n" %(i,name))
    i+=1
fo.close()
print "Content Written to File"


fo = open("studlist.txt","r")
print "Student Name List\n"
print "RollNo\tName"
for line in fo:
    print line,
            
Result: Thus a python program is written to perform file operations.