【问题标题】:Import and Export using OS in Python在 Python 中使用 OS 导入和导出
【发布时间】:2020-01-07 07:20:28
【问题描述】:

我想使用路径导入所有数据,但一直出错。以及如何在单独的文件中获得输出?

输入文件:

import os,json
path= "./Responses"

for file in os.listdir(path):
    with open (file) as json_file:
        data=json.load(json_file)

错误信息:

FileNotFoundError: [Errno 2] No such file or directory: 'data_1.json'

我可以使用“Out”分隔新数据吗?

输出文件:

for file in os.listdir(path):
    with open(json_data, 'w') as outfile:
        json.dump('Out'+ data, outfile, indent=4)     

【问题讨论】:

    标签: python arrays json


    【解决方案1】:

    出现错误的原因是python脚本找不到“with open (file) as json_file:”中创建的文件的相对路径

    要解决这个问题,您可以将其更改为:

     with open (path +'/'+ file) as json_file:
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 2023-01-26
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      相关资源
      最近更新 更多