【发布时间】:2019-11-26 05:55:09
【问题描述】:
我正在处理 .json 到 .csv 的转换。我正在从文件夹中读取 .json 文件并将其拆分,然后将结果写入同一文件夹中。
我想要的是将这些结果文件写入不同的文件夹。
new_path = 'C:/Users/toc/Desktop/Python_Codes/Data/Input'
name = askopenfilename(initialdir="C:/Users/toc/Desktop/Python_Codes/Data/JSON_File",
filetypes=(("Json File", "*.json"), ("All Files", "*.*")),
title="Choose a file."
)
try:
with open(name,'r', encoding='utf8') as infile:
o = json.load(infile)
chunkSize = 1
for i in range(0, len(o), chunkSize):
with open(name + '_' + str(i//chunkSize) + '.json', 'w') as outfile:
json.dump(o[i:i+chunkSize], outfile)
finally:
print("No file exists")
上面的代码是工作文件,我唯一需要知道的是如何将这些多个.json文件写入另一个文件夹,即new_path
【问题讨论】:
标签: python python-3.x file path