Android How to create a new json file if it doesn't exist python -


i creating app lets user enter information , can save data json file called hello.json. works fine when file exists, when testing on android crashed because not contain file. how can create new json file on android if doesn't exist?

.py file

from kivy.app import app kivy.lang import builder kivy.uix.popup import popup kivy.uix.button import button kivy.graphics import color, rectangle kivy.uix.boxlayout import boxlayout kivy.uix.floatlayout import floatlayout kivy.uix.image import asyncimage kivy.uix.label import label kivy.properties import stringproperty, listproperty kivy.uix.behaviors import buttonbehavior kivy.uix.textinput import textinput kivy.network.urlrequest import urlrequest kivy.storage.jsonstore import jsonstore os.path import exists kivy.compat import iteritems kivy.storage import abstractstore json import loads, dump kivy.config import config    class phone(floatlayout):     def __init__(self, **kwargs):         # make sure aren't overriding important functionality         super(phone, self).__init__(**kwargs)      self.canvas.before:         color(0, 1, 0, 1)  # green; colors range 0-1 instead of 0-255         self.rect = rectangle(size=self.size, pos=self.pos)      self.bind(size=self._update_rect, pos=self._update_rect)      open('hello.json') infile:         try:             data = phone.load(self)         except keyerror:             data = []  def _update_rect(self, instance, value):     self.rect.pos = instance.pos     self.rect.size = instance.size  def product(self, instance):     self.result.text = str(float(self.w.text) * 703/ (float(self.h.text) * float(self.h.text)))  def save(self):     store = jsonstore('hello.json')     name = self.n.text     gender = self.g.text     dtype = self.t.text     height = self.h.text     weight = self.w.text     bmi = self.result.text     medications = self.m.text     insulin = self.ti.text     store.put('profile', name=name, gender=gender, dtype=dtype, height=height, weight=weight, bmi=bmi, medications=medications, insulin=insulin)    def load(self):     store = jsonstore('hello.json')     profile = store.get('profile')     self.n.text = profile['name']     self.g.text = profile['gender']     self.t.text = profile['dtype']     self.h.text = profile['height']     self.w.text = profile['weight']     self.result.text = profile['bmi']     self.m.text = profile['medications']     self.ti.text = profile['insulin']      try:         store.get('profile')['name']     except keyerror:         name = ""     else:         name = store.get('profile')['name']             presentation = builder.load_file("main.kv")  class phoneapp(app):     def build(self):         store = jsonstore('hello.json')          return phone()    if __name__ == '__main__':     phoneapp().run() 

you should use with open('hello.json', 'a+') infile

a+ opens file both appending , reading. file pointer @ end of file if file exists. file opens in append mode. if file not exist, creates new file reading , writing.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -