python - IO.UnsupportedOperation: Not Writable -


i have made program stores score attached name in .txt file. however, want print alphabetical order of name attached score. when run program come error io.unsupportedoperation: not writable here code:

            file = open(class_name , 'r')       #opens file in 'append' mode don't delete information             name = (name)             file.write(str(name + " : " )) #writes information file             file.write(str(score))             file.write('\n')             linelist = file.readlines()             line in sorted(linelist):                 print(line.rstrip());                 file.close() 

you have opened file read only, attempted write it. file left open, attempt read it, if in append mode, file pointer @ end of file. try instead:

class_name = "class.txt" name = "joe" score = 1.5 file = open(class_name , 'a')       #opens file in 'append' mode don't delete information name = (name) file.write(str(name + " : " )) #writes information file file.write(str(score)) file.write('\n') file.close() file = open(class_name , 'r')  linelist = file.readlines() line in sorted(linelist):     print(line.rstrip()); file.close() 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -