I have a json
file from which the Python script reads key and value pairs and stores in notes
class variable. One of the key in notes
is called project_suites
. I am trying to duplicate this project_suites
key multiple times depending on sub-directories count in the script location. For each duplicate, I am including input_path
and output_path
:
# project_folders contains list of sub-directories
for idx, path in enumerate(project_folders):
# relative_path gets path without the test_path
relative_path = os.path.relpath(path, test_path)
# split the relative_path into a list
path_list = os.path.normpath(relative_path).split(os.path.sep)
# create new sub-key and assign path_list
notes.values['project_suites'][idx]['input_path'] = path_list
notes.values['project_suites'][idx]['output_path'] = path_list
print(notes.values['project_suites'][idx])
This seems to be working with the above print
in the loop outputs the following:
{'recursive': True, 'project': 'p.prj', 'grid': 'grid.i', 'input_path': ['01-zdir', '01-no-drag'], 'output_path': ['01-zdir', '01-no-drag']}
{'recursive': True, 'project': 'p.prj', 'grid': 'grid.i', 'input_path': ['01-zdir', '02-const-drag'], 'output_path': ['01-zdir', '02-const-drag']}
{'recursive': True, 'project': 'p.prj', 'grid': 'grid.i', 'input_path': ['01-zdir', '03-WenYu-drag'], 'output_path': ['01-zdir', '03-WenYu-drag']}
After the above for
loop, I am doing
print(notes.values['project_suites'])
This gives me the following output:
[{'recursive': True, 'project': 'p.prj', 'grid': 'grid.i', 'input_path': ['01-zdir', '03-WenYu-drag'], 'output_path': ['01-zdir', '03-WenYu-drag']}, {'recursive': True, 'project': 'p.prj', 'grid': 'grid.i', 'input_path': ['01-zdir', '03-WenYu-drag'], 'output_path': ['01-zdir', '03-WenYu-drag']}, {'recursive': True, 'project': 'p.prj', 'grid': 'grid.i', 'input_path': ['01-zdir', '03-WenYu-drag'], 'output_path': ['01-zdir', '03-WenYu-drag']}]
Basically, the input_path
and output_path
seem to have only the last path_list
for everyone. Any reason why this inconsistency is occurring?