Home > Net >  How to change files in the sequential order of the loop
How to change files in the sequential order of the loop

Time:10-03

I have 25 json files in a folder, named 0.json through 24.json, and I am trying to batch open and rename a perimeter "image" inside of each, which currently all have a placeholder of "https://" in the "image" field.

The .json currently appears as follows for each json file:


{"image": "https://", "attributes": [{"trait_type": "box color", "value": "blue"}, {"trait_type": "box shape", "value": "square"}]}

but should be

{"image": "https://weburlofnewimage/0", "attributes": [{"trait_type": "box color", "value": "blue"}, {"trait_type": "box shape", "value": "square"}]}

I have a central folder on a site like dropbox, that has a url structure of https://weburlofnewimage/0, /1, /2 etc. And so I would like to open each file, and change the value of the "image" key to be replaced with "https://weburlofnewimage/ current file number '.png'".

So far I am able to iterate through the files and change the image perimeter successfully within the json files, however the files seem to iterate in a random order, so on loop 1, I am getting file 20, and as a result file 20 is given file 0's image url.

Code as follows:

import json
import os

folderPath = r'/path/FolderWithJson/'
fileNumber = 0

for filename in os.listdir(folderPath):
    print('currently on file '   str(fileNumber))
    if not filename.endswith(".json"): continue
    filePath = os.path.join(folderPath, filename)
 
    with open(filePath, 'r ') as f:
        data = json.load(f)
        data['image'] = str('https://weburlofnewimage/'   str(fileNumber)   '.png')
    print('opening file '   str(filePath))
    
    os.remove(filePath)
    with open(filePath, 'w') as f:
        json.dump(data, f, indent=4)
    print('removing file '   str(filePath))

    fileNumber  =1

Which results in me getting the following printouts:

currently on file 10 (on loops 10)
currently preparing file 2.json (its working on file #2...)
opening file /path/FolderWithJson/2.json
removing file /path/FolderWithJson/2.json

And then when I look in 2.json I see the image is changed to "https://weburlofnewimage/10.png" instead of "https://weburlofnewimage/2.png"

CodePudding user response:

You can open a file with a direct path, instead of iterating through the directory. I would use a for loop to insert the numbers into the path, that way they iterate in order.

for fileNumber in range(0,24):
  with open(f'my_file/{fileNumber}.json') as f:
    ...doMyCode...

CodePudding user response:

Just pull the number from the file name. Don't use your own count. And please remember you never need to use the str function on a string. Many people seem to be getting that bad habit.

import json
import os

folderPath = '/path/FolderWithJson/'

for filename in os.listdir(folderPath):
    if not filename.endswith(".json"): 
        continue
    fileNumber = os.path.splitext(filename)[0]
    print('currently on file', fileNumber)
    filePath = os.path.join(folderPath, filename)

    print('opening file', filePath)
    with open(filePath, 'r') as f:
        data = json.load(f)
        data['image'] = 'https://weburlofnewimage/' fileNumber  '.png'

    print('rewriting file', filePath)
    with open(filePath, 'w') as f:
        json.dump(data, f, indent=4)
  • Related