1234567891011121314151617181920212223 |
- def save_dict_as_json(data: dict, path: str = os.getcwd(),
- filename: str = f'/temp_files/lay_name{random.randint(1, 1000)}') -> str:
- if path.__contains__('.json'):
- path_save = path
- elif filename.__contains__('.json'):
- path_save = filename
- elif filename.__contains__('.'):
- path_save = path + '/' + filename.split('/')[-1]
- else:
- path_save = path + f'/{filename}.json'
- path_save.replace("\\", '/')
- path_save.replace('//', '/')
- try:
- json_file = open(path_save, mode='x')
- except FileNotFoundError:
- os.mkdir(path_save.split('lay_name')[0])
- json_file = open(path_save, mode='x')
- except FileExistsError:
- json_file = open(path_save, mode='w')
- json.dump(data, json_file)
- json_file.close()
- return path_save
|