I have to convert a folder containing 1010 folders with a mixture of XML
, JSON
, TEXT
files to .CSV
file.
my CODE
# Import Module
import pandas
import os
json_file_path = "//Users//ruzi//Desktop//top1000_complete"
csv_file_path = "//Users//ruzi//Desktop//output.csv"
if os.path.isdir(json_file_path):
for e in os.listdir(json_file_path):
new_path=json_file_path "//" str(e)
if str(e) != '.DS_Store'and os.path.isdir(new_path):
for e1 in os.listdir(new_path):
if str(e1) != '.DS_Store'and os.path.isdir(next_new_path):
for e2 in os.listdir(next_new_path):
if str(e2) != '.DS_Store'and os.path.isfile(final_path):
f=open(final path,"rt")
# Read Json File
data_frame = pandas.read_json(json_file_path)
# Convert Into CSV
data_frame.to_csv(csv_file_path, index = None)
The full source code is shown below:
https://i.stack.imgur.com/1iED9.png
CodePudding user response:
You are getting a NotADirectory
error.
You should first examine if the path is a directory or not.
import os
#checks if path is a file
is_file = os.path.isfile(file_path)
#checks if path is a directory
is_directory = os.path.isdir(file_path)
Do something like this to check whether the path is a directory or a file:
import pandas
import os
json_file_path = "//Users//ruzi//Desktop//top1000_complete"
csv_file_path = "//Users//ruzi//Desktop//output.csv"
if os.path.isdir(json_file_path):
for e in os.listdir(json_file_path):
new_path = json_file_path "//" str(e)
if str(e) != '.DS_Store' and os.path.isdir(new_path):
for e1 in os.listdir(new_path):
next_new_path = new_path "//" str(e1)
if str(e1) != '.DS_Store' and os.path.isdir(next_new_path):
for e2 in os.listdir(next_new_path):
final_path = next_new_path "//" e2
if str(e2) != '.DS_Store' and os.path.isfile(final_path):
f = open(final_path,"rt")