0
I have a central dictionary that gets all jsons files from my working directory:
def loaddata():
with os.scandir('Jsons/') as it:
for entry in it:
if entry.is_file() and entry.name.endswith('.json'):
pass
with open(entry.path, 'r') as f:
key = entry.name[:-5]
value = f.read()
center_dict[key] = json.loads(value)
I need this dictionary to be accessible to all the other ".py files" I have in the directory. I can import functions between these scripts normally with:
import funcao from arquivo
.
Yet when I try to do the same for that dictionary
ImportError: cannot import name 'dict_teste' from 'script'
.
What I’m doing for now is calling loaddata()
for each of the scripts however this takes too long and ends the dynamism of the application. Do you know any way to turn this dictionary into something that can be accessed for all scripts or some other way to deal with the problem ?
****** Edit ******
This central dictionary is an instruction dictionary. It needs to be read by each of the scripts, however it will not be modified at any time.
I believe you have to create a function that takes the dictionary as parameter. Then you call this function in the desired python file
– Nicolas Pereira
some idea of a function that takes this dictionary from my main script and turns it into a global variable from my secondary script ?
– Filipe Gonçalves
Will these scripts run at the same time? If so, should the changes made to the dictionary by one reflect on the others? And if two scripts change the same position at the same time?
– Woss
Yeah, one script calls the other. I do not change the dictionary at any time in the code however each code needs a specific part of that dictionary. For example my main.py needs the dict_central['main'], my second.py needs the dict_central['second'] ... Your third question I didn’t understand.
– Filipe Gonçalves