Advertisement
ALEXANDAR_GEORGIEV

json_file_io

Nov 22nd, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | Source Code | 0 0
  1. import json
  2. from os.path import exists
  3.  
  4.  
  5. class JsonFileIo:
  6.     def __init__(self, *, file_name=None):
  7.         if file_name is None:
  8.             raise "You must provide file_name!"
  9.  
  10.         # if not exists(file_name):
  11.         #    raise "File missing!"
  12.  
  13.  
  14.         self.__file_name = file_name    # Превръщане на името на файла в свойство на обекта !
  15.  
  16.  
  17.     def read_content(self):
  18.  
  19.         if not exists(self.__file_name):
  20.             return None
  21.  
  22.         with open(self.__file_name, 'r', encoding="utf-8") as json_f:
  23.             content = json.load(json_f)
  24.  
  25.         return content
  26.  
  27.     def write_content(self, data):
  28.         #conv_json = json.dumps(data, ensure_ascii=False)
  29.         with open(self.__file_name, 'w', encoding="utf-8") as json_file:
  30.             json.dump(data, json_file,ensure_ascii=False)
  31.  
  32.  
  33. if __name__ == "__main__":
  34.     f1 = JsonFileIo(file_name="filename")
  35.     data = f1.read_content()
  36.     f1.write_content(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement