Python: Read several json files from a folder

后端 未结 5 767
梦谈多话
梦谈多话 2020-12-04 19:50

I would like to know how to read several json files from a single folder (without specifying the files names, just that they are json files).

Also, it

5条回答
  •  天命终不由人
    2020-12-04 20:11

    To read the json files,

    import os
    import glob
    
    contents = []
    json_dir_name = '/path/to/json/dir'
    
    json_pattern = os.path.join(json_dir_name, '*.json')
    file_list = glob.glob(json_pattern)
    for file in file_list:
      contents.append(read(file))
    

提交回复
热议问题