Home > Software engineering >  How can i open json files in folder and read them or print them in python
How can i open json files in folder and read them or print them in python

Time:10-11

i want to iterate and check if a new files are updates in the folder then read the files e.g. data.json then access the data and print them . but I cant seem to access the data inside the file it self

error message:with open(json_files) as file: TypeError: expected str, bytes or os.PathLike object, not list

import os, json
import pandas as pd

path_to_json = '/Webservertesting/JsonFiles'
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')] #creats a lis of the files in folder
print(type(json_files))  # for me this prints ['foo.json']

for jsonfile in json_files:
    print(jsonfile)
    with open(jsonfile,json) as file:
            data = json.load(file)
            print(data)

CodePudding user response:

Try this:

for jsonfile in json_files:
    print(jsonfile)
    with open(os.path.join(path_to_json, jsonfile)) as file:
            data = json.load(file)
            print(data)
  • Related