xor
xor

Reputation: 2698

file handling in kivy

I am using kivy for android application.

Currently I am working on a small game...the problem arises when I want to store the highest score in a file.... I am using python's open and readline and writelines but I am always getting the same error

"NO SUCH FILE OR DIRECTORY FOUND 'score.txt'"

but I have a file score.txt in the same directory where main.py is located.

I just cant understand if I am doing something wrong or file handling is impossible in kivy... also if there is any other way to do this then please tell me. Thanks

def read():
fr=open("score.txt","r")
line = fr.readline()
fr.close()
h=line.split(':')
high = int(h[1])
if high<disp_score:
    fw=open("score.txt","w")
    seq="highest:" + str(disp_score)
    fw.writelines(seq)
    fw.close()

this is my file implementation where only on line is written like highest:4000

Upvotes: 1

Views: 1832

Answers (1)

Tshirtman
Tshirtman

Reputation: 5947

You assume the current workind directory is the one where your main.py file is, that may not be the case, depending on the plateform, you can use the usual python methods to have a way to the directory main.py is, or you can use the kivy facilities (App.directory), or you can create the file if it doesn't exist (instead of shipping one) and stop caring about where it lives.

Anyway, be sure to do some testings about update of your application if you want to avoid the file to be destroyed on upgrade, putting it in the app may not be the best idea.

Upvotes: 3

Related Questions