0

The filename will be different every time, but the naming convention is the same.

Based on this solution, I want to open a filename of this example format:

20211201-095143.json

I don't want it to get confused with:

20211201-095143_metadata.json

Code:

import os
import glob

filenames = glob.glob(''.join([os.listdir(DIR), '*-*.json']))
filenames

with open(filenames[0]) as json_file:
    data = json.load(json_file)
    print(data)

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f1a73e787764> in <module>
----> 1 filenames = glob.glob(''.join([os.listdir(DIR), '*-*.json']))
      2 filenames

TypeError: sequence item 0: expected str instance, list found

Please let me know if there is anything else I should clarify.

StressedBoi69420
  • 1,376
  • 1
  • 12
  • 40
  • 1
    I think you meant to use `glob.glob(os.path.join(DIR, "*.json"))`, you get a list from that command and you can for example use regex or if you always have the keyword `metadata` even simple list comprehension to exclude files containing metadata – Bijay Regmi Dec 15 '21 at 12:51
  • Working now Thanks – StressedBoi69420 Dec 15 '21 at 12:55

0 Answers0