Create a directory and exist ok:
import pathlib
# create parent of a file
pathlib.Path('file_path').parent.mkdir(parents=True, exist_ok=True)
# create directory
pathlib.Path('directory_path').mkdir(parents=True, exist_ok=True)Return structure of a dictionary object (without all the values):
import typing
def return_dict_structure(obj):
"""Return the structure of a possibly nested dictionary object."""
new_obj = {}
if isinstance(obj, typing.List) or isinstance(obj, typing.Tuple):
if obj:
return [return_dict_structure(obj[0]), '...']
else:
return []
elif isinstance(obj, typing.Dict):
for k, v in obj.items():
new_obj[k] = return_dict_structure(v)
else:
return type(obj)
return new_obj