0

I'm looping through many folders to get any JSON file in each folder using the following code:

def get_all_jobs():
    for root_dir, _, file_names in os.walk(r'path'):
        for file_name in file_names:
            if file_name.endswith('.json'):
                all_files = (f'{root_dir}/{file_name}')
                for file in all_files:
                    with open(file_name, 'r', encoding="utf8") as json_file:
                        read_content = json.loads(json_file.read())

and I get this error :

FileNotFoundError: [Errno 2] No such file or directory:

and don't have one path to one folder to give but I have many folders in which I have the files. How can I solve this?

Nouman
  • 6,947
  • 7
  • 32
  • 60
RIG Code
  • 25
  • 1
  • 4
  • you can use glob module to escape all the directories and take just files ending with .json. check this related answer https://stackoverflow.com/a/50721824/6798902 – Murali Aug 05 '19 at 10:26
  • When asking questions about errors, always include the (copy-pasted, as text) *full* and *complete* error output. You should also include details like *what* file you attempt to open. Also please take some time to read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mcve]. – Some programmer dude Aug 05 '19 at 10:27
  • `for file in all_files` will iterate over the characters in the string created in the line above it. – N Chauhan Aug 05 '19 at 10:31

1 Answers1

0

find the information about glob here. glob will escape all the inside directories and match our pattern recursively.

def get_all_jobs():    
    for json_file in glob.iglob(path+"/**/*.json".replace('/',os.path.sep),recursive=True):
        with open(json_file, 'r', encoding="utf8") as jf:
            read_content = json.loads(jf.read())

Note: here path is the base directory where you have multiple folders which has your json files.

Explanation :

Here glob goes to your base directory path , from there recursively it goes to all sub folders and checks if any file contains .json extension, If it has, then it gives complete path of that file.

Murali
  • 364
  • 2
  • 11
  • I still get the same error because I don't have just one path to the files. I have a path to a folder which contains many folders, and each folder has another folder that has the files. so I can't give a path here "for json_file in glob.iglob(path+"/**/*.json".replace('/',os.path.sep),recursive=True" – RIG Code Aug 05 '19 at 11:43
  • you don't need to provide path for individual files, in my code 'path' is a base directory which has multiple folders and multiple jsons. so /**/ will wildcard all the sub directories and *.json will wildcard the filenames. so in the end you will get complete path to a json file in json_file. – Murali Aug 06 '19 at 07:47
  • i used the code you provided and i still didn't fix the error "No such file or directory:" my code def get_all_jobs(): for root_dir, _, file_names in os.walk(path): for file_name in file_names: if file_name.endswith('.json'): all_files = (f'{root_dir}/{file_name}') for json_file in glob.iglob(r'path'"/**/*.json".replace('/',os.path.sep),recursive=True): with open(file_name, 'r', encoding="utf8") as jf: read_content = json.loads(jf.read()) print(read_content) – RIG Code Aug 06 '19 at 09:39
  • You don't have to use os.walk and checking if it ends with .json also, I'm editing my answer. I assume 'path' is your base directory? – Murali Aug 06 '19 at 10:44
  • Did this post solve your question? if so, please don't forget to [accept](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) it, just click on the check mark to its left. – Murali Aug 20 '19 at 06:58