Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- from os.path import exists
- class JsonFileIo:
- def __init__(self, *, file_name=None):
- if file_name is None:
- raise "You must provide file_name!"
- # if not exists(file_name):
- # raise "File missing!"
- self.__file_name = file_name # Превръщане на името на файла в свойство на обекта !
- def read_content(self):
- if not exists(self.__file_name):
- return None
- with open(self.__file_name, 'r', encoding="utf-8") as json_f:
- content = json.load(json_f)
- return content
- def write_content(self, data):
- #conv_json = json.dumps(data, ensure_ascii=False)
- with open(self.__file_name, 'w', encoding="utf-8") as json_file:
- json.dump(data, json_file,ensure_ascii=False)
- if __name__ == "__main__":
- f1 = JsonFileIo(file_name="filename")
- data = f1.read_content()
- f1.write_content(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement