简体   繁体   中英

How can i open json files in folder and read them or print them in python

i want to iterate and check if a new files are updates in the folder then read the files eg 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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM