Python将JsonL转换为CSV文件权限错误13

Python将JsonL转换为CSV文件权限错误13,python,json,csv,type-conversion,jsonlines,Python,Json,Csv,Type Conversion,Jsonlines,我目前正在编写一个将jsonl转换为csv格式的脚本。但是,在visual studio代码的终端上运行代码时,出现以下错误: 回溯(最近一次呼叫最后一次): 文件“C:\Users\Natthanon\Documents\Coding 101\Python\test.py”,第24行,在 将open(r'C:\Users\Natthanon\Documents\Coding 101\Python\CSV','a',newline='')作为f: PermissionError:[Errno 1

我目前正在编写一个将jsonl转换为csv格式的脚本。但是,在visual studio代码的终端上运行代码时,出现以下错误:

回溯(最近一次呼叫最后一次):
文件“C:\Users\Natthanon\Documents\Coding 101\Python\test.py”,第24行,在
将open(r'C:\Users\Natthanon\Documents\Coding 101\Python\CSV','a',newline='')作为f:
PermissionError:[Errno 13]权限被拒绝:“C:\\Users\\Natthanon\\Documents\\Coding 101\\Python\\CSV”
下面是我的python脚本。如果有人知道我为什么会收到如上所示的权限错误,请告诉我是否有任何解决方案。我是Python新手,希望有经验的人能够帮助我解决这个问题。谢谢

导入全局
导入json
导入csv
导入时间
开始=时间。时间()
#作为pd进口熊猫
从展平\u json导入展平
#jsonl文件的路径
文件路径=(r'C:\Users\Natthanon\Documents\Coding 101\Python\JSONL')
#读取所有jsonl文件
files=[glob.glob(文件路径+“***.jsonl”,递归=True)中的f代表f]
i=0
对于文件中的f:
以开放式(f,'r')作为f:
对于F中的行:
#展平json文件
data=json.loads(第行)
数据_1=展平(数据)
#创建csv文件
将open(r'C:\Users\Natthanon\Documents\Coding 101\Python\CSV','a',newline='')作为f:
thewriter=csv.writer(f)
#header应该是构成Coulmn header的json文件的键值
writer.writerow([data_1['header1'],data_1['header2']))
似乎是的复制品

您试图做的是将目录作为文件打开,这将失败。 我猜您可以尝试以下操作:为jsonl上的每个.jsonl创建一个新的csv-on-csv文件夹

import glob
import json
import csv

import time


start = time.time()
#import pandas as pd
from flatten_json import flatten

#Path of jsonl file
File_path = (r'C:\Users\Natthanon\Documents\Coding 101\Python\JSONL')
#reading all jsonl files
files = [f for f in glob.glob( File_path + "**/*.jsonl", recursive=True)]
i=0
for f in files:
    with open(f, 'r') as F:
        for line in F:
#flatten json files 
            data = json.loads(line)
            data_1=flatten(data)
#creating csv files  
            with open(r'C:\Users\Natthanon\Documents\Coding 101\Python\CSV\\' + f.split("\\")[-1] +".csv", 'a' , newline='') as csv_file:
                thewriter = csv.writer(csv_file)
#headers should be the Key values from json files that make Coulmn header
                thewriter.writerow([data_1['header1'],data_1['header2']])
在线

 with open(r'C:\Users\Natthanon\Documents\Coding 101\Python\CSV\\' + f.split("\\")[-1] +".csv", 'a' , newline='') as csv_file:
您将获取jsonl文件的名称(拆分是为了除去所有路径,只是为了获取文件名),并在“CSV”文件夹上创建一个扩展名为.CSV的pair文件