file_1_3.py 830 B

1234567891011121314151617181920212223
  1. def save_dict_as_json(data: dict, path: str = os.getcwd(),
  2. filename: str = f'/temp_files/lay_name{random.randint(1, 1000)}') -> str:
  3. if path.__contains__('.json'):
  4. path_save = path
  5. elif filename.__contains__('.json'):
  6. path_save = filename
  7. elif filename.__contains__('.'):
  8. path_save = path + '/' + filename.split('/')[-1]
  9. else:
  10. path_save = path + f'/{filename}.json'
  11. path_save.replace("\\", '/')
  12. path_save.replace('//', '/')
  13. try:
  14. json_file = open(path_save, mode='x')
  15. except FileNotFoundError:
  16. os.mkdir(path_save.split('lay_name')[0])
  17. json_file = open(path_save, mode='x')
  18. except FileExistsError:
  19. json_file = open(path_save, mode='w')
  20. json.dump(data, json_file)
  21. json_file.close()
  22. return path_save