Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to read specific JSON files using python glob

i have a set of JSON files in a folder.

Sample files:

-2022_06_13_07_14_f71cd512135bdab9.json
-2022_06_13_07_1421_f71cd512135bdab9.json
-2022_06_13_07_12314_f71cd512135bdab9.json
-2022_06_14_132_14_f71cd512135bdab9.json
-2022_06_14_74647_14_f71cd512135bdab9.json

Instead of reading all files at once, I need to read them day-wise.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ex:2022_06_13_07_14_f71cd512135bdab9.json corresponding to 2022_06_13.

like wise I need to read all the JSON files and do the changes in Daywise batches.

read all JSON files in 2022_06_13 first then all the JSON files in 2022_06_14 and so on. i thought to apply wildcard with looping day wise list.

my issue with the below line. How do I add a wild card to find all JSON files related to a particular date?

json_files = glob.glob(os.path.join(path_to_json, 'day*.json'))

current code:

start = datetime.datetime(2022, 8, 25)
end = datetime.datetime(2022, 12, 25)
datelist = pd.date_range(start, end)
path_to_json = r'C:\Users\Admin\INPUT'

for a in datelist:
    day=str(a)[:10].replace('-', '_')
    json_files = glob.glob(os.path.join(path_to_json, 'day*.json'))
    
    for i in json_files:
        with open(i,'r') as fi:
            dict = json.load(fi)
            dict[0]["Name"] = "NETFLIX"
            fi.close()
        l= i.rsplit('\\', 1)[1]
        Output_URL="C:\\Users\\Admin\\OUTPUT\\Netflix\\"+l
        with open(Output_URL, "w+") as f:
            json.dump(data, f)

>Solution :

Change this:
json_files = glob.glob(os.path.join(path_to_json, 'day*.json'))

to this:
json_files = glob.glob(os.path.join(path_to_json, f"{a.strftime('%Y_%m_%d')}*.json"))

This will take date a and turn it into a string that is similar to the one in your files. Leave the asterisk after it as a wildcard and you should get all files that match date a everytime.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading