How to change files in the sequential order of the loop

Meowsleydale

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"

Tim Roberts

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to guarantee sequential order with angular http rest api in for loop?

How to compare 2 files having random numbers in non sequential order?

How to force robot framework to pick robot files in sequential order?

How to loop through a sequential series of files in a folder in R

How to change the order in which files are listed by bash

How to change the order of files when opening on VisualStudio?

How do I batch files numbered/named in sequential order starting from 0 using ffmpeg?

SAS SQL Loop Inputting Sequential Files

Powershell - Sequential foreach loop zipping files

How to execute a loop with sequential summation?

How to copy sequential files in Ubuntu

XSLT:How to Maintain Sequential Order of tag in XML

How to execute Vertx Future in sequential order

How to rename dictionary keys to follow sequential order?

How do I read images in their sequential order?

How to make multiple API calls in sequential order

How to use RxJS observables in sequential order?

How to run test in sequential order in loadimpact?

How to reshape a dataset in order to make it sequential?

How to tell ffmpeg to loop through all files in directory in order

How can I change this line in order to eliminate for loop?

How to change the polynomial order in a for-loop using the poly R function?

How do I change the order in which Meteor loads Javascript files?

What do I need to change in order to get Sequential output?

Loop to execute an order to multiple files

How to return value in sequential forEach loop?

How to loop through non-sequential array?

NodeJS - how to make forEach and for loop functions sequential

How to write a sequential for loop with conditional lookup capability