Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 reading, modifying and writing json file
#1
Hi Python Experts,

I am trying to open a json file, modify the content and write it to a different directory.
The code runs but nothing is really happening.

I am not sure what should come first, the dump or the misc save. Is the dump a save as well?


from scipy import ndimage, misc
import numpy as np
import os
import cv2
import json

def main():
    outPath='C:/..../'
    path='C:/...../data_aug_test/'

    # iterate through the names of contents of the folder
    for file_path in os.listdir(path):
	
	#create the full input path and read the file
        input_path = os.path.join(path, file_path)
	
        if file_path.endswith('.jpg'):	

             print('jpeg process')
		
    else: #.json
         print('else json handling')
		 
         with open(input_path, 'r') as f:
             data = json.load(f)
             data['id'] = 134 # <--- modify value.
			 
		    #write file then save	 
             jsonsavepath = os.path.join(outPath, 'flipped_'+file_path)
             misc.imsave(jsonsavepath, f)
			 
         with open(input_path, 'w') as f:
             json.dump(data, f, indent=4)	 
		 
if __name__ == '__main__':
     main()


Quote
#2
don't forget that:
for file_path in os.listdir(path):
will get you everything in path, including directories
Can't run your code as is, so don't know if this is an issue.
It could be in the future, so still not a good idea.

if running python 3.5 or newer, you can use pathlib (untested code):
>>> from pathlib import Path
>>> path Path(C:/Users/HOLLEYP/Downloads/TSS_DonkeyCarChallenge/tubs/tubs_cleaned/data/data_aug_test/)
>>> files = [x for x in home.iterdir() if x.is_file()]
>>> for file_path in files:
...     do stuff here
>>>
metalray likes this post
Quote
#3
OK. Thanks. Got it.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Help batch converting .json chosen file to MySQL BrandonKastning 2 145 Mar-14-2020, 09:19 PM
Last Post: BrandonKastning
  reading tab file Mandiph 1 351 Sep-05-2019, 01:03 PM
Last Post: ThomasL
  save my sensor data from the bme680 into a json or csv file Plastefuchs84 1 471 Aug-23-2019, 03:04 AM
Last Post: Plastefuchs84
  Reading a .dat file in Python mohd_umair 4 11,481 Apr-24-2019, 12:07 PM
Last Post: mohd_umair
  Merge JSON files prioritizing the updated values from most recent file nebulae 0 668 Apr-17-2019, 10:15 AM
Last Post: nebulae
  HELP! writing to csv file repeats, but code looks good. TheBeast 2 425 Mar-26-2019, 10:09 PM
Last Post: TheBeast
  JSON file Loading issue punna111 2 1,742 Aug-27-2018, 08:35 PM
Last Post: rootVIII
  Reading time steps from nc file ankurk017 1 780 Jul-16-2018, 07:06 PM
Last Post: woooee
  Pandas/Excel, reading from one column writing back to another... williamlombard 1 815 Mar-01-2018, 06:02 AM
Last Post: williamlombard
  Reading json file as pandas data frame? Alberto 1 5,649 Feb-05-2018, 12:43 AM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)