Home > Blockchain >  Modify json attribute with Python but getting FileNotFoundError: [Errno 2] No such file or directory
Modify json attribute with Python but getting FileNotFoundError: [Errno 2] No such file or directory

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.

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'".

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"}]}

So far I have tried the following, but I believe I am going down a rabbit hole

import json
import os

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

for filename in os.listdir(folderPath):
    with open(filename, 'r') as f:
        data = json.load(f)
        data['id'] = str('https://weburlofnewimage/'   str(fileNumber)   '.png')
    os.remove(filename)
    with open(filename, 'w') as f:
        json.dump(data, f, indent=4)

    fileNumber  =1

and have tried:

import os
import json

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

for filename in os.listdir(folderPath):
    with open(filename, 'r ') as f:
        data = json.load(f)
        data['image'] = str('https://weburlofnewimage/'   str(fileNumber)
        f.seek(0)
        json.dump(data, f, indent=4)
        f.truncate()     # remove remaining part
        fileNumber  =1

        

However, in both cases I get the following error:

FileNotFoundError: [Errno 2] No such file or directory: '20.json'

However, the file is indeed in that directory...

CodePudding user response:

You have to specifie a path to json file if it isn't in derictory with python file. Example:

import json
import os

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

for filename in os.listdir(folderPath):
    with open(/path/FolderWithJson/json.json, 'r') as f:
        data = json.load(f)
        data['id'] = str('https://weburlofnewimage/'   str(fileNumber)   '.png')
    os.remove(/path/FolderWithJson/json.json)
    with open(/path/FolderWithJson/json.json, 'w') as f:
        json.dump(data, f, indent=4)

    fileNumber  =1

CodePudding user response:

The filename variable only holds the name of the file in the folder you are iterating through, not the full path to that file. You need to specify the full absolute path to your file, or a relative path from where you're running the program.

Using os.path.join will combine the folder relative path you defined and the filename in that folder

for filename in os.listdir(folderPath):
    filePath = os.path.join(folderPath, filename)
    with open(filePath, 'r ') as f:
        # and so on

Also, (as you have found out) assuming that every file in a folder will be a json is unsafe. You should also add a guard to verify it's a .json.

for filename in os.listdir(folderPath):
    if not filename.endswith(".json"): continue
    filePath = os.path.join(folderPath, filename)
    with open(filePath, 'r ') as f:
        # and so on
  • Related