I'm currently working on a script that converts a jsonl to csv format. However, upon running the code on visual studio code's terminal, I get the following error:
Traceback (most recent call last):
File "C:\Users\Natthanon\Documents\Coding 101\Python\test.py", line 24, in <module>
with open(r'C:\Users\Natthanon\Documents\Coding 101\Python\CSV', 'a' , newline='') as f:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Natthanon\\Documents\\Coding 101\\Python\\CSV'
This is my python script below. If anyone has any clue on why I am receiving the permission error as shown above, do let me know if there are any solutions to this. I'm quite new to Python and I hope someone experienced will be able to help me out with this issue. Thanks!
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', 'a' , newline='') as f:
thewriter = csv.writer(f)
#headers should be the Key values from json files that make Coulmn header
thewriter.writerow([data_1['header1'],data_1['header2']])
Seems a duplicated of PermissionError: [Errno 13] in python.
What you are trying to do is to open a directory as a file, which will fail. Guessing you could try something like: create a new csv on CSV folder for every .jsonl on JSONL.
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']])
On line
with open(r'C:\Users\Natthanon\Documents\Coding 101\Python\CSV\\' + f.split("\\")[-1] +".csv", 'a' , newline='') as csv_file:
you are taking the name of the jsonl file (the split is to get rid of all the path and just to get the filename) and creating on the "CSV" folder a pair file with a .csv extension.