I have a directory full of python pickles containing dicts and I want to open the pickles in memory with their base name. If I have them in memory then I do not have to open and close the same dict(s) multiple times within other nested loops.
So
list_of_pickles = os.listdir(path/to/pickles)
but
for i in list_of_pickles:
pickle_base = i.rsplit('.')[0]
name_pickle = f'{path/to/pickles}/{i}'
with open(name_pickle,'rb') as current_pickle:
pickle_base = pickle.load(current_pickle)
fails to assign the pickles to their base names and assign them to pickle_base
literally. Being able to assign to the pickles' base name would make things easier.
I did it with locals()[{variable_name}]
. So, if one wants something assigned to the variable's string content instead of the variable's literal name use locals()[{variable_name}]
.
For example, in the above code:
for i in list_of_pickles:
pickle_base = i.rsplit('.')[0]
name_pickle = f'{path/to/pickles}/{i}'
with open(name_pickle,'rb') as current_pickle:
locals()[pickle_base] = pickle.load(current_pickle)