def sorting(): myfile=open('namelist.txt') al=myfile.readlines() myfile.close() al.sort() myfile=open('namelistsorted.txt','w') for x in al: myfile.write(x) myfile.close sorting()