find the extension of a json file in python

To find the extension of a JSON file in Python, you can use the os module to extract the file extension from the file path. Here's an example code snippet:

main.py
import os

filename = '/path/to/your/file.json'
file_extension = os.path.splitext(filename)[1]

if file_extension == '.json':
    print('File is a JSON file')
else:
    print('File is not a JSON file')
202 chars
10 lines

In this code, the os.path.splitext() function splits the file path into the file name and its extension. The [1] index returns the file extension string (including the .). We then check if the file extension is .json to determine if it is a JSON file.

gistlibby LogSnag