Skip to content
Advertisement

Python iterate folder of csv and convert do json

I am amateur at python but I have a task of converting folder of csv to json files. I have this script working with specified CSV file but I have no idea how to make the script iterate thrue folder of csv and convert all of those csv to json. The original script:

JavaScript

someone will give me a hint?

Advertisement

Answer

You can use os functions, particularly os.listdir() to iterate over files in the directory, and safely generate new names with os.path.splitext():

JavaScript

The similar approach with pathlib would be:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement