I have a directory with JSON files that all have the same structure. I would like to loop through them and replace the values of key 'imgName' with the basename of the filename + 'png'. I managed to do that, however, when I dump the content of my dictionaries in the JSON files, they are empty. I'm sure there is some problem with the logic of my loops, but I can't figure it out.
Here is my code:
distorted_json = glob.glob('...')
for filename in distorted_json:
with open(filename) as f:
json_content = json.load(f)
basename = os.path.basename(filename)
if basename[-9:] == '.min.json':
name = basename[0:-9]
for basename in json_content:
json_content['imgName'] = name + '.png'
for filename in distorted_json:
with open(filename, 'w') as f:
json.dumps(json_content)
Thank you very much in advance for any advice!
basename[-9:] == '.min.json': name = basename[0:-9], I think you could have created a suffix variable (suffix = ".min.json"), and then, used this variable to do the other 2 operations, such as...if basename.endswith(suffix): name = basename[0:len(suffix)]. That way, you only need to change the suffix variable, and your program would still work as intended. :)