Skip to content

Instantly share code, notes, and snippets.

@AntaeusNar
Created January 4, 2019 17:02
Show Gist options
  • Select an option

  • Save AntaeusNar/1d715a2bef4b4563ad63725c83be2018 to your computer and use it in GitHub Desktop.

Select an option

Save AntaeusNar/1d715a2bef4b4563ad63725c83be2018 to your computer and use it in GitHub Desktop.
JSON file saving and reading
import json
def file_path_name_w_ext(file_name, path=None):
if path is None:
file_path = './'
else:
file_path = './' + path + '/'
if file_name.endswith(".json"):
path_name_w_ext = file_path + file_name
else:
path_name_w_ext = file_path + file_name + '.json'
return path_name_w_ext
def writeJSONfile(file_name, data, path=None):
with open(file_path_name_w_ext(file_name, path), 'w') as fp:
json.dump(data, fp)
def getJSONfile(file_name, path=None):
with open(file_path_name_w_ext(file_name, path), 'r') as fp:
return json.load(fp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment