find the name of a json file in python

To find the name of a JSON file in Python, you can use the os module to navigate through directories and the json module to load the contents of the file. Here is an example code snippet to find the name of a JSON file:

main.py
import os

# set the path of the directory containing the JSON file
path = '/path/to/directory'

# get a list of all the files in the directory
files = os.listdir(path)

# loop through each file in the directory
for file in files:
    # check if the file has a .json extension
    if os.path.splitext(file)[1] == '.json':
        # print the name of the file
        print(file)
379 chars
15 lines

This code will first set the path of the directory containing the JSON file, then use the os.listdir() function to get a list of all the files in that directory. It will then loop through each file in the directory, check if the file has a .json extension using the os.path.splitext() function, and finally print the name of the file that has a .json extension.

Note that this code assumes that there is only one JSON file in the directory with a .json extension. If there are multiple JSON files in the directory, you may need to modify the code to handle that case.

gistlibby LogSnag