I have a folder with 2000 json files and I have to edit specific line with number based on file name.
files names :-
1.json
2.json
3.json
...
1999.json
2000.json
each file contain text like below:-
1.json
{
"Name": "Testname #5",
"Description":
}
2.json
{
"Name": "Testname #5",
"Description":
}
Currently no is not in correct order. I want to make like this :
1.json
{
"Name": "Testname #1",
"Description":
}
2.json
{
"Name": "Testname #2",
"Description":
}
100.json
{
"Name": "Testname #100",
"Description":
}
I have done something like this
import os
import sys
import fileinput
x = 0;
for line in fileinput.input(['1.json'], inplace=True):
if line.strip().startswith('"name"'):
line = '"name":"Testname #' + str(x) + '",\n'
x += 1
sys.stdout.write(line)
this is works for 1 file at a time. I need to do bulk edit. can anyone help me
json.load(f)rather than manual parsing. When dealing with all the files, you probably need to write to some temporary file first e.g.1-temp.json, process all files, then finally delete the old files and rename the new temp files.