You can use auxiliary variable, initialize it to 1 before your first "files" for statement and use it to create filename and increase after use.
for dirpath, dirname, ... file_number = 1 for filename in .. .... new_filename = "{:03d}.json".format(file_number) # you can use just str() + ".json", but format adds more options file_number += 1 newfilepath = os.path.join(dirpath, new_filename) ...If you have some old files with same name, it will overwrite them, so be careful. And with "numerical" file names its sometimes useful to zero-fill number, so they are sorted naturally (no 1.json, 11.json, ..19.json, 2.json, but 001.json, 002.json...).